-1

#include<iostream>
using namespace std;

class Nokta{
    private:
        int x,y;

    public:
        Nokta();
        Nokta(int, int);

        int getX();
        int getY();

        void setX(int);
        void setY(int);

      

};


Nokta::Nokta(){
    cout << "parametresiz kurucu cagrildi\n";
}


Nokta::Nokta(int x, int y=0){
    this->x = x;
    this->y = y;
    cout << "parametreli kurucu cagrildi\n";
}

int Nokta::getX(){
    return x;
}
int Nokta::getY(){
    return y;
}
void Nokta::setX(int _x){
    x=_x;
}
void Nokta::setY(int _y){
    if(_y > 5)
        y=_y;
    else
        y = 2;
}

int main(){  
 
    Nokta *ptr;
    cout  << ptr << " " << &ptr << " "<< ptr->getX() << endl;
  
    return 0;
}

Nokta* ptr; A constructor function is not called when I type it, but I can print one of its variables to the screen. ptr->getX() works. I guess this value is randomly assigned, but how is it done in the background before an object is created?

Output 0x401b6b 0x61ff0c 1528349827

Gamze
  • 9
  • 1
  • 2
  • 2
    `ptr` is uninitialized, so `ptr->getX()` has **undefined behavior**. – Remy Lebeau Jan 16 '22 at 10:05
  • 3
    It has _undefined behavior_, meaning that there is no guarantee how the program will behave. It could do anything for any reason or none. Btw. already printing `ptr` is undefined behavior, because `ptr` is not initialized to any value. – user17732522 Jan 16 '22 at 10:05
  • 1
    Yes, the constructor won't be called, because you're just declaring a pointer to Nokta class. If you want the constructor to be called, allocate some memory to it using `new` operator. – kiner_shah Jan 16 '22 at 10:06
  • 1
    `Nokta *ptr;` does not allocate a `Nokta`. Try `Nokta n;` `cout << n.getX() << endl;` instead. That also has _undefined behavior_ though since you haven't initialized `x` in the default constructor. – Ted Lyngmo Jan 16 '22 at 10:07
  • 1
    Does this answer your question? [C++ function called without object initialization](https://stackoverflow.com/questions/5360501/c-function-called-without-object-initialization), https://stackoverflow.com/questions/32963735/why-doesnt-accessing-an-uninitialized-pointer-display-an-error?noredirect=1&lq=1, https://stackoverflow.com/questions/6278139/object-creation-differences?noredirect=1&lq=1 – user17732522 Jan 16 '22 at 10:09

1 Answers1

0

ptr is just a variable that should hold an address to a Nokta object. But you did not create a Nokta object for the pointer to point to.

Nokta noktaObj = Nokta(someNum, someNum)

Nokta* ptr = &noktaObj

When you don't initialize the object, and try to access data (like ptr->getX()), you're just getting whatever garbage happens to be in memory in that location.

Brandon O.
  • 101
  • 5
  • 1
    Your code doesn't compile. `new` returns a pointer. Also, there is no guarantee that accessing the pointer will produce some garbage value. The program might just as well crash or consistently produce a value of `0`. It is undefined behavior. – user17732522 Jan 16 '22 at 11:47
  • Yes, my mistake I did not mean to add the `new` statement. – Brandon O. Jan 16 '22 at 18:39
  • That is correct, it is undefined behavior. I didn't want to use any terms like that as it seems like OP is still green, just wanted to explain conceptually what is happening. It is undefined because you are accessing memory that has not been initialized. Basically grabbing some leftover garbage in the memory that can do anything to your program. – Brandon O. Jan 16 '22 at 18:45