Reputation: 11651
I am currently confused with the following statement - I though this statement would yield an error during compile time however it doesn't.
// statement 1:
someclass* q(someclass());
I understand if the statement was like this
// statement 2:
someclass* q(&someclass());
I would like to know why statment 1 doesnt generate an error or even if that is valid (is there anything I am missing behind the scenes ?)
Upvotes: 5
Views: 166
Reputation: 126432
I would like to know why statment 1 doesnt generate an error or even if that is valid
The first statement is valid, although it is probably not doing what you expect: this statement is declaring a function named q
which returns a pointer to an object of type someclass
and takes in input a function which in turn accepts no arguments and returns an object of type someclass
. This is called the Most Vexing Parse.
The second statement is not valid: it is trying to declare a pointer named q
to an object of type someclass
, and initialize this pointer to the address of the object constructed by the someclass()
expression. Notice, however, that someclass()
is a temporary, and taking the address of a temporary is illegal.
Upvotes: 12
Reputation: 40603
Statement 1 is actually a declaration for a function. This function is called q
, and takes a pointer to a function taking no arguments and returning a someclass
, and returns a pointer to someclass
.
See Most Vexing Parse.
Upvotes: 3