找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 4590|回复: 1

关于运算符重载的问题

[复制链接]

1

主题

0

回帖

0

精华

初来乍到

积分
12
发表于 2012-3-27 21:21:28 | 显示全部楼层 |阅读模式
#include <iostream>
using namespace std;
class A
{
        int a,b,c ;
public:
        A(int x=0,int y=0,int z=0)
        {
                a=x,b=y,c=z;
        }
        A operator+(A t)
        {
               
                a+=t.a;
                b+=t.b;
                c+=t.c;
                return *this;
        }
        /*friend A operator-(A t1,A t2)
        {
                A temp;
                temp.a=t1.a-t2.a;
                temp.b=t1.b-t2.b;
                temp.c=t1.c-t2.c;
                return temp;
        }*/
        void operator+=(A t)
        {
                a+=t.a;
                b+=t.b;
                c+=t.c;
        }
        friend void operator-=(A& t1,A t2)
        {
                t1.a-=t2.a;
                t1.b-=t2.b;
                t1.c-=t2.c;
               
        }
        void print()
        {
                cout<<a<<'\t'<<b<<'\t'<<c<<'\n';
        }

};

void main()
{
        A a1(1,2,3),a2(4,5,6),a3,a4;
        a3=a1+a2;
        //a4=a1-a2;
        a1+=a3;
        a2-=a4;
        a1.print();
        a2.print();
        a3.print();
        a4.print();
}
我想知道加法运算重载时this指针到底是怎么样的?还有为什么第一个友元函数在编译时报错?。。。小女子谢了

857

主题

2632

回帖

2

精华

管理员

此生无悔入华夏,  长居日耳曼尼亚。  

积分
36130
发表于 2014-9-13 13:09:49 | 显示全部楼层
这种问题建议去专门讨论C++的论坛问吧。。。
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表