17
#include <iostream>
using namespace std;

class CTest 
{
    int x;

  public:
    CTest()
    { 
       x = 3;
       cout << "A"; 
    }
};

int main () {
  CTest t1;
  CTest t2();

  return 0;
}

CTest t1 prints "A" of course.

But it seems like nothing happens at t2(), but the code runs well.

So do we use those parentheses without argument? Or why can we use it this way?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
balinta
  • 566
  • 1
  • 6
  • 18
  • It's [the most vexing parse](http://en.wikipedia.org/wiki/Most_vexing_parse) in C++ – Peter Wood Feb 28 '12 at 22:03
  • @PeterWood: Personally, I dispute that. Historically the "most vexing parse" referred only to cases where the ambiguity occurs with an initializer that is a value initialized temporary, e.g. `CTest t2(CTest());` rather than this simpler case. – CB Bailey Feb 28 '12 at 22:05
  • @CharlesBailey 'related to the most vexing parse' would have been a better way of putting it. Thanks. – Peter Wood Feb 29 '12 at 07:46

1 Answers1

34

This is a quirk of C++ syntax. The line

CTest t1;

declares a local variable of type CTest named t1. It implicitly calls the default constructor. On the other hand, the line

CTest t2();

Is not a variable declaration, but a local prototype of a function called t2 that takes no arguments and returns a CTest. The reason that the constructor isn't called for t2 is because there's no object being created here.

If you want to declare a local variable of object type and use the default constructor, you should omit the parentheses.

In C++11, you can alternatively say

CTest t2{};

Which does actually call the default constructor.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
  • 9
    You should note the difference between `CTest t1; CTest t2 = CTest();` – Martin York Feb 28 '12 at 23:20
  • @MartinYork What's the difference between them? The latter copies, while the former does not? (Maybe typical compilers would treat them the same though) – starriet Jul 24 '22 at 13:12
  • 1
    @starriet: `CTest t1;` as above. `CTest t2 = CTest();` the correct way (in 2012) to explicitly use the default constructor (rather than be a forward declaration as above). A now in 2022 `CTest t2{};` – Martin York Jul 25 '22 at 21:36