#include "stdafx.h"
#include "iostream"
class CMan
{
private:
    int height;
    int age;
public:
    int getheight()const;
    int getage()const;
    CMan();
    ~CMan();
    CMan operator +(CMan& one );
};CMan CMan::operator +(CMan& one )   //这个地方看不懂
{    
    CMan temp;
    temp.age = age+one.getage();
    temp.height = height+one.getheight();
    return temp;
}CMan::CMan()
{
    height = 100;
    age= 10;
}CMan::~CMan()
{}int CMan::getage()const
{
    return age ;
}int CMan::getheight()const
{
    return height;
}int main(int argc, char* argv[])
{
    CMan man1,man2,man3;
    man3= man1+man2;
    printf("%d",man3.getage());
    printf("\n");
    printf("%d",man3.getheight());
    return 0;
}说明(执行顺序): /*
1,主函数先构造3个CMan对象, 分别为man1,man2, man3.
2,然后,通过man3=man1+man2后, 进入运算符重载函数 CMan CMan::operator +(CMan& one )   //这个地方看不懂
3,在第2步骤中, 再次构造对象temp, 最后将其返回。
4,不明白的地方是:
temp.age = age+one.getage();
temp.height = height+one.getheight();
中的age+one.getage()具体表示什么意思? 是哪一个是man1的age成员, 哪一个又是man2的age成员。
同理:height+one.getheight()也是一样的迷惑!!!
敬请回复, 谢谢!!!
*/

解决方案 »

  1.   

    再添加个构造函数,就好区分了#include "iostream"class CMan
    {
    private:
    int height;
    int age;
    public:
    int getheight()const;
    int getage()const;
    CMan();
    CMan::CMan(int a, int b);// 添加一个构造函数
    ~CMan();
    CMan operator +(CMan& one );
    };CMan CMan::operator +(CMan& one )   //这个地方看不懂
    {    
    CMan temp;
    temp.age = age+one.getage();
    temp.height = height+one.getheight();
    return temp;
    }CMan::CMan()
    {
    height = 100;
    age= 10;
    }CMan::CMan(int a, int b)
    {
    height = a;
    age= b;
    }
    CMan::~CMan()
    {}int CMan::getage()const
    {
    return age ;
    }int CMan::getheight()const
    {
    return height;
    }int main(int argc, char* argv[])
    {
    CMan man1,man2(1,2),man3;
    man3= man1+man2;
    printf("%d",man3.getage());
    printf("\n");
    printf("%d",man3.getheight());
    return 0;
    }
      

  2.   

    或者改为这样,也好懂点儿CMan CMan::operator +(CMan& one )   //这个地方看不懂
    {    
    age = age+one.getage();
    height = height+one.getheight();
    return *this;
    }
      

  3.   

    temp.height = height+one.getheight();
    中的age+one.getage()具体表示什么意思? 是哪一个是man1的age成员, 哪一个又是man2的age成员。
    ---------------------------------------
    第一个age是man1的age的值,第二个调用得到的是man2的age的值。这个你Debug下调试看一下就清楚了
      

  4.   

    在成员函数operator+时候,+号左边是this,+号右边是第一个参数,=号左边是返回值