Is it allowed to instantiate a class without specified variable name in C++ -
this question has answer here:
- most vexing parse 1 answer
i have these 2 classes:
class foo { public: foo() { std::cout << "in foo constructor" << std::endl; } }; class bar { public: bar() {}; bar(foo foo); private: foo m_foo; }; bar::bar(foo foo) : m_foo(foo) { std::cout << "in bar constructor argument foo" << std::endl; } int main() { bar bar(foo()); // wrong here ? return 0; }
i compiled , excuted , nothing printed on screen, did bar bar(foo())
? have seen similarity in do parentheses after type name make difference new? , foo f = foo(); // no matching function call 'foo::foo(foo)', still can't figure out.
to achieve same without changing semantics (ie. not using copy or assignment constructor), add parentheses disambiguate syntax (the compiler not know if declare function locally or if build object, default former prioritized c++ standard):
int main() { bar bar ( (foo()) ); return 0; }
as pointed out in wikipedia page posted in comments, issue c++ grammar itself, not can it.
Comments
Post a Comment