Possible Duplicates:
Calling the default constuctor
Why is it an error to use an empty set of brackets to call a constructor with no arguments?
I am wondering what it means to create an instance of a class using a no arguments constructor.
For example if I have a class "A" and try to create a variable like so:
A myVariable()
I tried to put together a little project to test it:
#include <iostream>
using namespace std;
class A
{
public:
A();
A( int a, int b );
public:
int mA;
int mB;
private:
virtual void init( int a , int b );
};
A::A()
{
cout << "\tA()" << endl;
this->init( 0, 0 );
} // end of function A::A
A::A( int a, int b = 0 )
{
cout << "\tA( int a, int b )" << endl;
this->init( a, b );
} // end of function A::A
void A::init( int a, int b )
{
cout << "\tInit( int a, int b )" << endl;
mA = a;
mB = b;
} // end of function A::init
int main()
{
cout << "Start" << endl;
cout << "A myA0()" << endl;
A myA0();
cout << "A myA1" << endl;
A myA1;
cout << "A myA2( 0 )" << endl;
A myA2( 0 );
cout << "A myA3( 0, 0 )" << endl;
A myA3( 0, 0 );
cout << "Stop" << endl;
return 0;
}
The output looks like this:
Start
A myA0()
A myA1
A()
Init( int a, int b )
A myA2( 0 )
A( int a, int b )
Init( int a, int b )
A myA3( 0, 0 )
A( int a, int b )
Init( int a, int b )
Stop
So it doesn't seem to call any constructor. When I try to step through it in Visual Studio it just skips over it like it wasn't compiled and when I try to print out the variable i get an unknown extern symbol error.
Note: Of course when using the new operator doing "new A()" is required and will use the default constructor.