0

I came into an issue when trying to declare an instance of my own class "myClass". For example, myClass class() gave a compile error.

I did some reading and now know why, because I am essentially declaring a function "class" that takes no arguments and returns type "myClass". I see this now. But what I dont understand is if I have an overloaded constructor, why doesn't the compiler think this: myClass class(argument) is me trying to declare a function "class" with one argument that returns type "myClass"?

Is it because there is no argument type, and then it knows its the overloaded constructor?

  • 2
    For what its worth, your question is the subject of the often discussed [most-vexxing-parse](http://en.wikipedia.org/wiki/Most_vexing_parse) – WhozCraig Mar 25 '13 at 00:03
  • @WhozCraig - this is **not** the "most vexing parse". It is simply a function declaration. The most vexing parse involves calling a function with a default-constructed temporary argument; the compiler treats the construction as an argument declaration (with redundant parentheses), and gives an error. – Pete Becker Mar 25 '13 at 12:59

3 Answers3

1

That's right. The arguments to a function declaration must be types, so if they aren't types, the compiler knows it cannot be a function declaration and instead assumes you are constructing an instance and passing the arguments as parameters. This logic works pretty well except in the special case where the constructor has no arguments, in which case it is ambiguous. C++ resolves this ambiguity by preferring to treat a statement as a declaration if possible, so to create an instance with no parameters you leave off the parameter list.

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
1

If argument is the name of a variable, or an expression that cannot be interpreted as a type, then there is no way for the compiler to interpret this as a function declaration - because in a function declaration you can omit the parameter names and specify only their type, but not vice versa.

However, if you have something like this:

myClass object(myOtherClass());

Which you may think of as an attempt to copy-construct an object called object from a default-constructed temporary of type myOtherClass, you would run into the so-called Most Vexing Parse: the compiler, in fact, will interpret the above as the declaration of a function called object that returns an object of type myClass and accepts as its only argument a function which, in turn, takes no argument and returns a value of type myOtherClass.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • So then for this you would want to omit the parentheses? myClass class(myOtherClass) <----- this would be correct? – user2205782 Mar 25 '13 at 01:24
  • @user2205782: That would be a function declaration again (of a function called `class` - btw, that's a keyword, so you should pick another name - which takes an object of type `myOtherClass` as its only argument and returns an object of type `myClass`). In C++11, you can solve the ambiguity by using the braces syntax instead of parentheses: for instance, `myClass object{}` or `myClass object{myOtherClass{}}` - the first default-constructs an instance of `myClass`, the second one constructs an instance of `myClass` supplying a default-constructed instance of `myOtherClass` as an argument. – Andy Prowl Mar 25 '13 at 08:34
1
myClass myclass()

declares a function with name myclass, return type myClass and no parameter. This is known as http://en.wikipedia.org/wiki/Most_vexing_parse

When you declare a class object that calls the default constructor, you do:

myClass obj;

The function's argument has to be some type.

myClass myclass(argument)

means creating an object of class myClass, with object name myclass.

myClass myclass(typename argument)

declares a function, it is not calling any constructor, typename is required, but argument is optional in function prototype.

taocp
  • 23,276
  • 10
  • 49
  • 62
  • Actually `myClass class()` won’t compile since `class` is a reserved word. – Konrad Rudolph Mar 25 '13 at 00:05
  • The example in your first line is not usually considered a "most vexing parse", afaik. That term is generally taken to mean the construction that Andy Prowl cites in his answer. – us2012 Mar 25 '13 at 00:13
  • @us2012 that example is also one instance of vex of parsing. see second answer to this post: http://stackoverflow.com/questions/2318650/is-no-parentheses-on-a-constructor-with-no-arguments-a-language-standard – taocp Mar 25 '13 at 00:18