-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
  • 246,999
  • 92
  • 370
  • 646
Subi Suresh
  • 281
  • 1
  • 3
  • 9

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
  • 38,538
  • 10
  • 94
  • 140
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
  • 16,846
  • 8
  • 48
  • 66