C++学习笔记5:运算符重载

一:基本概念

c++中的运算符(+ - * / ....< >  ... << >> ...)可以直接作用于基本类型(char ,int ,double ..)和标准库定义的类型(string...)
但是这些运算符不能直接作用于我们自定义的数据类型, 这个时候就要重新赋予这些运算符新的功能,使之能够直接操作我们自定义类型,这就是运算符重载。
c++中所有运算符重载都是使用函数实现的,所以运算符重载的本质就是函数重载。

运算符重载函数的格式如下:
 

  返回值类型 operator运算符号(参数列表)
    {
        具体代码
    }

    返回值类型:运算符的运算结果类型
    operator运算符号    ->相当于函数名
            比如: operator+
    参数列表:参数个数及参数类型
            参数个数:由该运算符所需要的操作数的个数决定
            参数类型:你想要参与该运算符的数据类型(一定要至少包含一个自定义类型

如:

Mystring& Mystring::operator=(const Mystring& other) {
    /* 新功能函数体 */
}

二、运算符重载的限制

1、不能重载的运算符(常见的)

sizeof :    sizeof运算符
.      :    成员运算符
.*     :    成员指针运算符
::     :    作用域运算符
? :    :    三目运算符

2、不能使用友元函数,只能声明为成员函数的运算符

=     :    赋值运算符
[]    :    下标运算符
()    :    函数调用运算符
->    :    通过指针访问类成员的运算符

三、运算符重载的两种方式

一、重载为成员函数

以mstring类举例:

/* 拷贝赋值函数 */
Mystring& Mystring::operator=(const Mystring& other) {
    assert(this != &other);
    this->m_size = other.m_size;
    delete []this->m_ptr;
    this->m_ptr = new char[this->m_size + 1]{0};
    strcpy(this->m_ptr, other.m_ptr);

    return *this;
}

/* 移动拷贝赋值函数 */
Mystring& Mystring::operator=(Mystring&& other) {
    assert(this != &other);
    this->m_size = other.m_size;
    delete []this->m_ptr;
    this->m_ptr = other.m_ptr;
    other.m_ptr = nullptr;

    return *this;
}

二、重载为友元函数

1、在class类中生明
using std::ostream;
class Mystring {

    friend ostream& operator<<(ostream& out, const Mystring& other);

}

2、实现

ostream& operator<<(ostream& out, const Mystring& other) {
    out << other.m_ptr;
    return out;
}

ps:特殊点的运算符(前++和后++)

对于a本身而言没有区别,区别在于这个表达式的值不一样,a++这个表达式的值是a自增之前的值,
    ++a这个表达式的值是a自增之后的值。

    ++这个运算符有前++和后++之分,重载一次显然满足不了要求,-> 需要重载两次:
        后++ 要提供一个“占位形参”,该形参只是占一个位置,在函数中并不会用到它

   class Demo
        {
        public:
            Demo(int data = 0)
            {
                this->data = data;
            }

            Demo& operator++()//前++
            {
                data ++;
                return *this;
            }

            Demo operator++(int)//后++
            {
                Demo temp{data};
                data ++;
                return temp;
            }
        private:
            int data;
        }

        int main()
        {
            Demo d{10};
            ++d;
        }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的高精度运算符重载是指对整数进行大数运算时,通过重载运算符来实现对大数的加减乘除等操作。一般情况下,C++内置的整数类型(如int、long等)有一定的位数限制,无法处理超过其表示范围的大数。而通过运算符重载,我们可以自定义一个类来表示大数,并对其进行各种运算操作。 以下是一个简单的示例,展示了如何实现C++中的高精度运算符重载: ```cpp #include <iostream> #include <vector> using namespace std; class BigInteger { private: vector<int> digits; // 用vector存储大数的每一位 public: BigInteger() {} BigInteger(int num) { while (num > 0) { digits.push_back(num % 10); num /= 10; } } BigInteger operator+(const BigInteger& other) const { BigInteger result; int carry = 0; int i = 0; while (i < digits.size() || i < other.digits.size() || carry != 0) { int sum = carry; if (i < digits.size()) { sum += digits[i]; } if (i < other.digits.size()) { sum += other.digits[i]; } result.digits.push_back(sum % 10); carry = sum / 10; i++; } return result; } friend ostream& operator<<(ostream& os, const BigInteger& num) { for (int i = num.digits.size() - 1; i >= 0; i--) { os << num.digits[i]; } return os; } }; int main() { BigInteger a(123456789); BigInteger b(987654321); BigInteger c = a + b; cout << "a + b = " << c << endl; return 0; } ``` 在上述示例中,我们定义了一个名为BigInteger的类,用于表示大数。通过重载加法运算符`+`,我们可以实现对两个BigInteger对象的相加操作。同时,我们还重载了输出流运算符`<<`,以便能够直接输出BigInteger对象的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值