【发布时间】:2017-08-10 19:53:29
【问题描述】:
输出是
构造函数调用
20
当我添加一个复制构造函数时,它给出了错误“invalid initialization of non-const reference of type of 'Foo&' from an rvalue of a rvalue”
#include <iostream>
using namespace std;
class Foo
{
int a;
public:
Foo(int a)
{
this->a =a;
cout<<"Constructor called\n";
}
void operator=(Foo f)
{
this->a = a;
cout<< "Assignment operator called";
}
void show()
{
cout<<this->a<<endl;
}
};
int main()
{
// your code goes here
Foo F1 = static_cast<Foo>(20);
F1.show();
return 0;
}
【问题讨论】:
标签: c++ copy-constructor assignment-operator