We know that compiler generates some member functions for user-defined class if that member functions are not defined but used, isn’t it. So I have this kind of code:
class AA
{
};
void main()
{
AA a;
AA b(a);
a = b;
}
This code works fine. I mean no compiler error. But the following code….
class AA
{
int member1;
int member2;
};
But this code gives an run time error, because variable “a” is used without being iniltialized!!!
So my question is this: when we instantiate an int, it has a value. So why the default constructer doesn’t work and by using those two int numbers initializes variable “a”??
EDIT: Platform: Win Vista, Compiler: Visual Studio 2008 compiler; Flags: Default
The compiler-synthesised default constructor calls the default constructors for all class members that have constructors. But integers don’t have constructors, and so are not initialised. However, I find it hard to believe that this will cause a run-time error.
To initialise those variables: