吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 973|回复: 2
收起左侧

[C&C++ 转载] C++关于重载运算符函数的个人理解思路

[复制链接]
li5021467 发表于 2021-3-16 11:50
//声明重载加号运算符方法   
        Integer operator+(Integer other);  //
        //提供一个IntValue方法返回计算的结果
        int IntValue() { return m_value; }
private:
        int m_value;
};
//初始化构造函数
Integer(int value) :m_value(value) {}      //这里是我们书写的构造函数,意思为将 传进来的value值赋给m_value,


/*初始化当调用构造函数的时候默认给m_value赋值为0*/
Integer::Integer() :m_value(0)
{

}
/*实现运算符重载的方法
*operator+可以把+看作一个函数名
*/
Integer Integer::operator+(Integer other) {   //这里是一个Integer 类型的一个型参
        Integer result(this->m_value+other.m_value); //用来返回的结果对象  this表示传入的第一个参数
        return result;
}
仔细看几段代码我是这么理解的:
//此处声明一个类Integer 类型的变量
Integer int1(100),int2(200),int3();   //这里的变量我们可以看作是 调用这个函数 函数名称为 int1,int2,int3.。。。 Integer(int value) :m_value(value) {}  《《《《《《《相当于把100 200 这个值赋给了value
int3 = int1 + int2;   //这里面的加我们其实可以看作成是一个函数,因为我们前面已经赋值了int1和int2,这段可以看作是   再一次把int1 的值和int2的值给了Integer Integer::operator+(Integer other) 这个函数里面
这是目前我对运算符重载的理解,希望大佬能指正。脑细胞已经死伤无数

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wangpaishi 发表于 2021-3-17 09:25
本帖最后由 wangpaishi 于 2021-3-17 09:29 编辑

我是菜鸟,借你代码温习一下C++,好多年不弄了,尽弄C#,HTML,JS,PHP,Android JAVA了
[C++] 纯文本查看 复制代码
#include <iostream>
using namespace std;

class Integer
{
public:
        Integer();
        Integer(int value);
        Integer(string strName);
        Integer(string strName, int value);
        void SetName(string strName);
        string GetName();
        
        // 声明重载"+"运算符   
        Integer operator+(const Integer& another);
        int Value() { return m_nValue; }
        
private:
        int m_nValue;
        string m_strName;
};

Integer::Integer()
        :Integer("")
{
}

Integer::Integer(string strName)
        :m_strName(strName), m_nValue(0)
{
        cout << "构造: 我是 "<< this->m_strName <<endl;
}

Integer::Integer(int value)
        :Integer("NoName", value)
{
}

Integer::Integer(string strName, int value)
        :m_strName(strName), m_nValue(value)
{
        cout << "构造: 我是 "<< this->m_strName <<endl;
}

void Integer::SetName(string strName)
{
        this->m_strName = strName;
}

string Integer::GetName()
{
        return this->m_strName;
}

/*实现运算符重载的方法*/
Integer Integer::operator+(const Integer& another)
{
        cout << "正在进行 + 运算: 我是 "<< this->m_strName << ",另一个是 " << another.m_strName <<endl;
        return Integer(this->m_nValue + another.m_nValue);
}

int main()
{
        Integer aa("aa", 100), bb("bb", 200), cc("cc");
        //aa.SetName("aa");
        //bb.SetName("bb");
        //cc.SetName("cc");
        
        cc = aa + bb;
        cout << "结果:"<< cc.GetName() << "=" << cc.Value() <<endl;
        return 0;
}

运行结果
[C++] 纯文本查看 复制代码
构造: 我是 aa
构造: 我是 bb
构造: 我是 cc
正在进行 + 运算: 我是 aa,另一个是 bb
构造: 我是 NoName
结果:NoName=300


这里的变量我们可以看作是创建Integer类对象并调用Integer类构造函数实例化了 aa,bb,cc,即形参构造函数Integer(int value)和默认构造函数Integer();相当于把100,200,默认(0)这个数值赋给了Integer类实例对象内的私有数据成员m_nValue
cc= aa + bb;   // 这里面的右侧加我们其实可以看作成是调用了aa的+函数,传入参数是bb,通过运算结果可以看出cc变量在求和赋值后已经是新构建的对象了
 楼主| li5021467 发表于 2021-3-16 15:35
个人的理解思路,不一定正确,但请大佬能指正
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-6 03:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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