65,186
社区成员




llwwzz@Ubuntu-llwwzz:~/test$ g++ -o t t1.cpp
t1.cpp: In member function ‘A& A::fc()’:
t1.cpp:19:9: error: invalid initialization of non-const reference of type ‘A&’ from an rvalue of type ‘A* const’
return this;
#include<iostream>
using namespace std;
class A
{
public:
A(int);
A & fc();
void show();
~A();
private:
int a;
};
A::A(int b):a(b){}
A & A::fc()
{
this->a += 2;
return this;
}
void A::show()
{
cout << a << endl;
}
A::~A()
{
cout << "~A()" << endl;
}
int main()
{
A a(2);
a.fc();
a.show();
return 0;
}