我想知道是否有人可以帮助我解决c++
(gcc
最新版本)中的奇怪行为。下面是一些成功编译的代码,由于缺少适当的构造函数,我预计会出现编译时错误。谁能解释发生了什么?
#include <iostream>
using namespace std;
struct ClassA {
ClassA() {cout << "hello" << endl;}
void speak() {cout << "I am class A" << endl;}
~ClassA() {cout << "goodbye" << endl;}
};
struct ClassB {
// just an empty struct
};
int main() {
ClassA myClassA(ClassB()); // trying to construct class A from an rvalue reference to class B is ok?
return 0;
}
}
如果我尝试调用 的函数class A
,我会得到一个编译时错误:
int main() {
ClassA myClassA(ClassB());
myClassA.speak();
return 0;
}
结果是:
error: request for member ‘speak’ in ‘myClassA', which is of non-class type ‘ClassA(ClassB (*)())’
谢谢!