【发布时间】:2014-06-19 13:34:11
【问题描述】:
我对 C++ 很陌生,并且观察到以下代码行的行为不同
MyClass c1;
c1.do_work() //works
MyClass c2();
c2.do_work() //compiler error c2228: left side is not a class, structure, or union.
MyClass c3{};
c3.do_work() //works
以头文件为
class MyClass {
public:
MyClass();
void do_work();
};
你能解释一下,这三种创建对象的方式有什么区别吗?以及为什么第二种方式会产生编译错误?
【问题讨论】:
-
其中两个:en.cppreference.com/w/cpp/language/initialization。至于第三个,请查看 Most Vexing Parse,您无疑会找到它。
-
这个问题的答案有帮助吗:stackoverflow.com/questions/9490349/…
-
我从未听说过最令人烦恼的 Parse...谢谢!
标签: c++ c++11 constructor most-vexing-parse