Hello Everybody,
I have a small doubt regarding how we can call constructors in a C++ class

class test
{
public:
       
      test()
      {
            cout<<"In test\n";
      }
};


int main() 
{
        test t1;
        test t2();    // This call does not give compile time error but does not call the constructor
	cin.get();
}

So my question is what exactly happens when the statement test t2() is executed. Why does this call not invoke the default constructor.

The compiler sees that line as a declaration of a function ( t2 ) which takes no arguments and returns an object of class test .

Got it.. thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.