I am using Visual Studio 2013.
I have the following code
class Foo{
public:
Foo(){};
Foo* fPtr;
float f;
};
int main()
{
Foo foo1; // No Warning. fPtr is initialized to NULL, f is set to 0.0.
Foo foo2(foo1); // Compiles fine. Uses compiler generated copy constructor
return 0;
}
Now consider the same code but without the user defined default constructor.
class Foo{
public:
// No user defined default constructor, or equivalently
// Foo() = default;
Foo* fPtr;
float f;
};
int main()
{
Foo foo1; // Neither fPtr nor f are initialized, both contain garbage.
//Foo foo2(foo1); // error C4700: uninitialized local variable 'foo1' used
return 0;
}
I thought that the user defined default constructor and the implicitly defined default constructor are equivalent, but it seems to me they are not.
How exactly does the compiler defined default constructor work?
After this case, I am cautious against using compiler defined default constructors and always provide my own, even though sometimes empty default constructors. Am I misunderstanding something?