Reputation: 429
struct A{
A(){}
};
struct B{
B(const A& a){}
};
int main()
{
//Originally I would like to write down below code
A a;
B b(a);
//Unfortunately I end up with below code by accident and there is no compile error
//I have figured out the below does not create temporary A and call B constructor to
//create B as the above codes,
//it declares a function with return value B, name b,
//and some input parameter, my question is 1) what the input parameter is ?
//2) How to implement such a function.
B b(A()); // There is no global function A() in my test case.
}
The question is in the comment, I hope some people can help me to understand it. Thank you very much.
Upvotes: 1
Views: 81
Reputation: 119467
It declares a function named b
that returns B
, which has a single parameter of type A (*)()
, that is, pointer to function taking no arguments and returning A
. The declarator A()
means "function taking no arguments and returning A
", but whenever you declare a parameter to have function type, it's rewritten to become a pointer to function. The parameter in this declaration is unnamed (you don't have to specify a name for a parameter if you don't want to).
To implement such a function you would need a definition, e.g.,
B b(A a()) {
// do something with "a"
// note: the type of "a" is still pointer to function
}
See, e.g., Is there any use for local function declarations?
Upvotes: 3
Reputation: 83567
B b(A())
declares a function named b
which returns a B
and takes a function pointer as an argument. The function pointer points to a function which returns a A
and takes no arguments.
Upvotes: 2