Reputation: 159
I see this code and I can't understand what it mean. I know how we use default constructor but this is not default constructor. What is this?
class X
{
...
};
int main()
{
X f();
}
Upvotes: 3
Views: 626
Reputation: 58271
Its function declaration of name f
X f();
^ ^ function
return type
function f()
takes no arguments and returns a X
class object.
for example its definition can be like:
class X{
int i;
// other definition
}
X f(){
X x;
// some more code
return x;
}
In main you can use like:
int main(){
X a = f();
int i = f().i;
}
Upvotes: 2
Reputation: 18411
Assume you declare a function:
int Random();
And use it:
int main()
{
int n;
n = Random();
}
But implement the Random function after main
. Or assume that Random
function is defined in some header. You need to instruct the compiler that Random
is a function implemented in some other source file, or in some library.
Therefore, an expression like:
T foo();
Would always mean a instruction to compiler that there is a function named foo
which returns T
. It cannot be an object of type T
.
Upvotes: 3
Reputation: 3588
This is a function which doesn't take any argument and returns an object of class X
Upvotes: 1
Reputation: 206526
It declares a function f
which takes no parameters and returns a type X
.
This is also known as Most Vexing Parse in C++. It is a byproduct of the way the C++ standard defines the interpretation rules for declarations.
Upvotes: 8