-2
class A {
  public:
    A() { cout << "Constructor\n"; }  // (1) default constructor
};

A obj;                                // (2) instantiating obj

A obj();                              // (3) 

What is the difference between instantiating obj and obj()? obj calls the default constructor (1) mentioned above. Which constructor will obj() call?

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
Subi Suresh
  • 291
  • 1
  • 4
  • 10

2 Answers2

1

A obj(); declares a function called obj which takes no arguments and which returns an A. It does not declare an A object at all.

As A obj(); does not declare an A object, it does not result in any constructor call.

Mankarse
  • 39,818
  • 11
  • 97
  • 141
0

The second one is declared as a function. the name of the function is obj. It takes no arguments. It returns the object of type A.

Apurv
  • 17,116
  • 8
  • 51
  • 67