I am getting compilation error C4700: uninitialized local variable 'obj' used as expected on below code because variable x and y are not initialize.
#include <iostream>
class Object
{
public:
float x;
float y;
};
int main()
{
Object obj;
std::cout << obj.x << std::endl;
std::cout << obj.y << std::endl;
}
But for following program is compiling successfully with garbage outputs on console.
#include <iostream>
class Object
{
public:
float x;
float y;
public:
float GetX(){return x;}
float GetY(){return y;}
};
int main()
{
Object obj;
std::cout << obj.GetX() << std::endl;
std::cout << obj.GetY() << std::endl;
}
Now question is x, y are still uninitialized but compiler doing partiality in second case. can someone will elloborate more this?