1
#include<iostream>

class _ctor
{
public:
_ctor() { std::cout<<"\nCtor";}
~_ctor(){ std::cout<<"\nDtor";}
};

_ctor A(); // --> Is the Constructor Really called? I do not see the Output printed
//_ctor A;

int main(){
return 0;
}

The Output of the above code is given in this Link I don't see the constructor getting called, what could be the problem?? If it is not supposed to be called then what does this mean _ctor A();?

Sadique
  • 22,181
  • 7
  • 62
  • 90
  • FYI the name `_ctor` is reserved which makes your program ill-formed (this has nothing to do with the question which has already been answered). More details in [this question](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – Motti Mar 21 '11 at 19:18

4 Answers4

10

You declared a function called A() which returns a _ctor, and you never called that function. You never even defined that function.

No, the constructor of _ctor is not being called.

Benjamin Lindley
  • 98,924
  • 9
  • 191
  • 266
  • 3
    See http://stackoverflow.com/questions/180172/why-is-it-an-error-to-use-an-empty-set-of-brackets-to-call-a-constructor-with-no – Martin Beckett Mar 21 '11 at 18:31
  • interesting how this keeps cropping up here, when I first read of this I thought it a corner case. – Ilkka Mar 21 '11 at 18:36
5

No, because you're actually declaring a function that takes no arguments and returns a _ctor. This is called "the most vexing parse." You probably want this:

_ctor A;
Community
  • 1
  • 1
Fred Larson
  • 58,972
  • 15
  • 110
  • 164
1

You are declaring a function named A that returns a ctor class object so no constructor is called.

If you want to create a global object of ctor class you can do:

_ctor A;

which calls the constructor.

codaddict
  • 429,241
  • 80
  • 483
  • 523
0

For the constructor to be able to call, the program needs to instantiate class _ctor.

_ctor A();

The above statement is a prototype for function A() saying that it's return type is _ctor.

Mahesh
  • 33,625
  • 17
  • 84
  • 113