Wednesday 14 March, 2007

Calling ctor!!

Well, suppose we have a class as below


class CTest
{
    public:
        CTest()
        {
            cout<<"ctor called!";
        }
};

int main()
{
    CTest();
}


Does it mean c'tor can be called without creating an object?

Nope!! ofcourse not!!

The reason is when CTest(); is called, the compiler inserts the code to create a tempory object of CTest and calls its c'tor. Also it destroys the object right after executing CTest(); statement.

That is the reason calling constructor inside another constructor is not valid.

2 comments:

Anonymous said...

Dude, you need to replace "private" access specifier with the "public" one, otherwise constructor call could not be invoked.

Prashanth Narayanaswamy said...

Ibrahim, Thanks for pointing out the mistake. It was a typo. Corrected the same :)