Take the 2-minute tour ×

i searched but could not find the answer.

So I have my c++ constructor:

MyClass(string username = "something");

note this is the only constructor I have.

in my main, I do:

MyClass one();
MyClass two = MyClass();

are these two expressions equivalent? is the compiler gonna call my constructor with the default string, or is it gonna call the default (empty) constructor?

What would change if I did have a constructor MyClass(); ? I guess that would not compile, right?

share|improve this question
MyClass one(); Refferred to as the most Vexing parse Its not actually a declaration of the variable one. But a forward declaration of a function called one. –  Loki Astari Mar 19 '11 at 7:27

4 Answers 4

MyClass one();

This declares a function one returning a MyClass object and taking no arguments.

If you have both default constructor (of the form MyClass()) and default argument constuctor (of the form MyClass(string s = "string") ) which one would be called if you don't pass any argument?

For example this would not compile

class MyClass
   MyClass(std::string username = "something") {}

int main()
   MyClass one();
   MyClass two = MyClass(); //ambiguous call here
share|improve this answer
Also, two = MyClass() will not compile because the type is missing. –  Jon Mar 19 '11 at 4:31
@Jon : I am assuming that he somehow missed the type. –  Prasoon Saurav Mar 19 '11 at 4:35
Unless it is previously declared in that function, or defined in the class. By itself, you're right. –  Merlyn Morgan-Graham Mar 19 '11 at 4:36
@Prasoon: I know -- it's just for the sake of completeness of the answer. –  Jon Mar 19 '11 at 4:38
"You can't have both default constructor and default argument constructor defined within a class." this is incorrect. and it's sort of fractally incorrect: both constructors in the OP's example are default constructors, and yes you can have two or more of them. it makes it practically impossible to call the one that doesn't have formal arguments, but it's OK to have it there. Please correct. –  Cheers and hth. - Alf Mar 19 '11 at 5:08
MyClass one;
MyClass two = MyClass();

If this is what was meant then one and two will call the same constructor, which happens to be your constructor, which happens to be the only constructor.

share|improve this answer

It will call your constructor. If you define a constructor yourself--any constructor regardless of the number of arguments--the compiler will not generate a default constructor.

[edit] As Prasoon Saurav says, MyClass one(); declares a function. You want MyClass one;, note: no parentheses.

share|improve this answer
ok, thanks everyone. –  Edoz Mar 19 '11 at 4:35

The expressions will have the same nett effect, but the aren't strictly equivalent. The first calls your constructor on one. The second constructs a temporary and then assigns it to two, thus invoking the class's copy-constructor.

share|improve this answer
While the copy-constructor call can be optimized away, it still has to be accessible. –  Ben Voigt Mar 19 '11 at 5:23

Your Answer


