#include<iostream> class name { public: int a; name():a(0){}; }; void add(name * pname) { pname = NULL; } int main() { name varName(); name * pName = new name(); add(pName); add(&varName);//error C2664: 'add' : cannot convert parameter 1 from 'name __cdecl *)(void)' to 'name *' }
#include<iostream> class name { public: int a; name():a(0){}; }; void add(name * pname) {
Share
I think it’s worth telling you about a similar problem, that also causes trouble:
What b really is is a function that returns a
barand takes as first argument a pointer to a function that returns a foo taking no arguments. It’s the same as:If you want to create a bar object initialized by a default constructed foo, put parentheses around the argument. That makes it doesn’t look like a function declaration anymore, and the compiler will interpret it like you want:
There are also non-obvious cases where a compiler error should be risen. GCC gets this wrong, but Comeau gets it right again. Consider the following snippet
You will probably expect that this takes the static constant, and casts it to
int, initializing thevvariable to0? No, it won’t according to the Standard, because the initializer can be interpreted as a declaration, according to pure syntax analysis, as the following showsWhenever the initializer can be interpreted as a declaration, in such a situation whole the declaration will declare a function. So, the line in
maindeclares a function like the following, omitting the redundant and meaningless parenthesesAnd this will cause a compiler error when parsing the function declaration, because a function parameter name may not be qualified.