#include <iostream>
#include <string>
using namespace std;
class Person
{
private:
string m_name;
int m_age;
public:
Person(string name, int age);
Person(const Person& rhs);
const Person operator *(const Person& rhs);
Person& operator=(const Person& rhs);
~Person();
};
Person::Person(string name, int age):m_name(name),m_age(age){
cout<<"construct "+name<<endl;
}
Person::Person(const Person& rhs){
m_name=rhs.m_name;
m_age=rhs.m_age;
cout<<"copy construct "+m_name<<endl;
}
const Person Person::operator*(const Person& rhs){
m_name+=rhs.m_name;
m_age+=rhs.m_age;
cout<<"operator *"<<endl;
return Person(m_name, m_age);
}
Person& Person::operator=(const Person& rhs){
m_name=rhs.m_name;
m_age=rhs.m_age;
cout<<"operator ="<<endl;
return *this;
}
Person::~Person(){
cout<<"deconstruct "+m_name<<endl;
}
int main ()
{
Person a("liu",12), b("wan", 12);
Person result=a*b;
cout<<endl;
result=result*b;
system("pause");
return 0;
}
vs2012 执行结果
cygwin 执行结果