我想将一个临时对象(例如 std::string)传递给我的对象的构造函数:
class MyClass{
public:
MyClass(string a):
a(a)
{
}
string a;
};
int main(int argc, char *argv[]){
MyClass a(string());
cout<<a.a<<endl;
return 0;
}
但我收到此错误:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:28:11: error: request for member ‘a’ in ‘a’, which is of non-class type ‘MyClass(std::string (*)()) {aka MyClass(std::basic_string<char> (*)())}’
如果我将任何内容传递给临时对象的构造函数(例如字符串(“”)),一切正常。为什么?