0
Class A
{
public:
    A()
    {
       cout << "constructor called";
    }
};

int main()
{
  A obj;     // constructor getting called
  A obj1();   // constructor not getting called
}

When i instantiate obj object , my constructor getting called. But when i instantiate obj1 , my constructor not getting called.

I would like to know the reason for it.

1 Answers1

0

A obj1(); actually does not create any object, it's a function declaration.

() should be avoided here as the compiler can't tell if you meant to create a A object or declare a obj1 function returning a A object.

jpo38
  • 20,821
  • 10
  • 70
  • 151