#include <iostream> using namespace std; class Test
{
public:
Test(int a, int b)
{
this->a = a;
this->b = b;
}
int getA()
{
return a;
}
int getB()
{
return b;
} Test add(Test &t2)
{
this->a = this->a + t2.getA();
this->b = this->b + t2.getB();
//return *this;
}
void print()
{
cout << "a = " << a << " b = " << b << endl; }
protected:
private:
int a, b;
}; Test add(Test &t1, Test &t2)
{
Test t3(t1.getA() + t2.getA(), t1.getB() + t2.getB());
return t3;
} int main()
{
Test t1(, );
Test t2(, );
//Test t3 = add(t1, t2);
t1.add(t2);
t1.print(); system("pause");
return ;
}
成员函数隐藏了一个this指针,谁调用成员函数,this指针就指向谁,这个this指针就是指向调用成员函数的对象的首地址.
在成员函数里, *this 就是调用对象元素本身.一般用于return *this