[知识点]c++运算符重载

在 C++ 中,运算符重载(Operator Overloading)允许你定义或修改运算符的行为,使其适用于用户定义的类型(例如类或结构体)。通过运算符重载,你可以使自定义类型与内置类型一样自然地使用运算符。

重载运算符的基本原则

  1. 不能创建新的运算符:只能重载已有的 C++ 运算符。
  2. 不能改变运算符的优先级和结合性:重载运算符的优先级和结合性与原运算符相同。
  3. 必须有一个用户定义类型的操作数:至少有一个操作数是用户定义类型(类或结构体)。
  4. 某些运算符不能被重载:例如,::(作用域解析运算符)、.*(成员指针访问运算符)、.(成员访问运算符)、?:(三元运算符)等。

运算符重载的方法

运算符可以作为成员函数或非成员函数(通常是友元函数)进行重载。

成员函数重载
  • 一元运算符:无参数。
  • 二元运算符:一个参数。
非成员函数(友元函数)重载
  • 一元运算符:一个参数。
  • 二元运算符:两个参数。

常见运算符重载示例

1. 重载赋值运算符 =

赋值运算符通常作为成员函数重载,用于实现深拷贝。

#include <iostream>
#include <cstring>

class String {
private:
    char* data;
public:
    String(const char* str = "") {
        data = new char[strlen(str) + 1];
        strcpy(data, str);
    }

    String(const String& other) {
        data = new char[strlen(other.data) + 1];
        strcpy(data, other.data);
    }

    ~String() {
        delete[] data;
    }

    String& operator=(const String& other) {
        if (this != &other) {
            delete[] data;
            data = new char[strlen(other.data) + 1];
            strcpy(data, other.data);
        }
        return *this;
    }

    void print() const {
        std::cout << data << std::endl;
    }
};

int main() {
    String str1("Hello");
    String str2 = str1;
    String str3;
    str3 = str2;
    str1.print();
    str2.print();
    str3.print();
    return 0;
}
2. 重载算术运算符 +
#include <iostream>

class Complex {
private:
    double real, imag;
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 成员函数重载
    Complex operator+(const Complex& other) const {
        return Complex(real + other.real, imag + other.imag);
    }

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2;
    c3.print();
    return 0;
}
3. 重载关系运算符 ==
#include <iostream>

class Complex {
private:
    double real, imag;
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 成员函数重载
    bool operator==(const Complex& other) const {
        return real == other.real && imag == other.imag;
    }

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(3.0, 4.0);
    if (c1 == c2) {
        std::cout << "c1 is equal to c2" << std::endl;
    } else {
        std::cout << "c1 is not equal to c2" << std::endl;
    }
    return 0;
}
4. 重载输入输出运算符 <<>>

输入输出运算符通常作为友元函数重载。

#include <iostream>

class Complex {
private:
    double real, imag;
public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    friend std::ostream& operator<<(std::ostream& os, const Complex& c) {
        os << "(" << c.real << ", " << c.imag << ")";
        return os;
    }

    friend std::istream& operator>>(std::istream& is, Complex& c) {
        is >> c.real >> c.imag;
        return is;
    }
};

int main() {
    Complex c1;
    std::cout << "Enter a complex number (real and imaginary part): ";
    std::cin >> c1;
    std::cout << "You entered: " << c1 << std::endl;
    return 0;
}

5. 重载数组下标运算符 []

#include <iostream>

class Array {
private:
    int* data;
    size_t size;
public:
    Array(size_t sz) : size(sz) {
        data = new int[size];
    }

    ~Array() {
        delete[] data;
    }

    int& operator[](size_t index) {
        return data[index];
    }

    const int& operator[](size_t index) const {
        return data[index];
    }

    size_t getSize() const {
        return size;
    }
};

int main() {
    Array arr(10);
    for (size_t i = 0; i < arr.getSize(); ++i) {
        arr[i] = i * 10;
    }
    for (size_t i = 0; i < arr.getSize(); ++i) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
    return 0;
}

6. 重载前置和后置递增递减运算符 ++--

#include <iostream>

class Counter {
private:
    int value;
public:
    Counter(int val = 0) : value(val) {}

    // 前置递增
    Counter& operator++() {
        ++value;
        return *this;
    }

    // 后置递增
    Counter operator++(int) {
        Counter temp = *this;
        ++value;
        return temp;
    }

    // 前置递减
    Counter& operator--() {
        --value;
        return *this;
    }

    // 后置递减
    Counter operator--(int) {
        Counter temp = *this;
        --value;
        return temp;
    }

    int getValue() const {
        return value;
    }
};

int main() {
    Counter c(5);
    std::cout << "Initial value: " << c.getValue() << std::endl;
    ++c;
    std::cout << "After prefix increment: " << c.getValue() << std::endl;
    c++;
    std::cout << "After postfix increment: " << c.getValue() << std::endl;
    --c;
    std::cout << "After prefix decrement: " << c.getValue() << std::endl;
    c--;
    std::cout << "After postfix decrement: " << c.getValue() << std::endl;
    return 0;
}

友元函数的作用
友元函数可以访问类的私有和保护成员,因此可以实现非成员函数的灵活性,同时保留对类内部状态的访问能力。

何时需要友元函数重载
对称性:对于某些运算符,尤其是二元运算符(如 +, -, *, /, ==, != 等),将运算符重载为友元函数可以实现更好的对称性,允许左操作数和右操作数分别是类对象和非类对象。例如,a + b 和 b + a 需要对称处理。
访问私有成员:当运算符需要访问类的私有或保护成员时,可以将其定义为友元函数,以便访问这些成员。
非成员函数:某些运算符(如输入输出运算符 << 和 >>)通常需要定义为非成员函数,因为左操作数是标准库类型(如 std::ostream 或 std::istream),不能作为类的成员函数来实现。

总结

将运算符重载为成员函数或友元函数都会对类的设计和使用产生不同的影响。理解这些影响可以帮助你做出最佳选择。以下是将运算符重载为成员函数和友元函数的一些影响和考虑。

成员函数重载的影响

  1. 调用方式的限制
  2. 访问权限
  3. 对称性
  4. 简化实现

1. 调用方式的限制 (主要差别)

成员函数重载运算符的一个主要限制是,它只能在类的实例作为左操作数时调用。这是因为成员函数隐含地接受类实例作为第一个参数。

示例:成员函数重载 +
#include <iostream>

class Complex {
private:
    double real, imag;

public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 成员函数重载 + 运算符
    Complex operator+(const Complex& rhs) const {
        return Complex(real + rhs.real, imag + rhs.imag);
    }

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2; // 合法,c1 是左操作数
    // Complex c4 = 1.0 + c1; // 非法,左操作数不是 Complex 类实例
    c3.print(); // 输出: (4.0, 6.0)

    return 0;
}

在这个例子中,c1 + c2 是合法的,因为 c1Complex 类的实例。但 1.0 + c1 是非法的,因为 1.0 不是 Complex 类的实例。

2. 访问权限

成员函数重载可以直接访问类的私有和保护成员,无需通过公共接口。这在某些情况下可以简化实现。

示例:成员函数重载 + 访问私有成员
#include <iostream>

class Complex {
private:
    double real, imag;

public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 成员函数重载 + 运算符
    Complex operator+(const Complex& rhs) const {
        return Complex(real + rhs.real, imag + rhs.imag);
    }

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2;
    c3.print(); // 输出: (4.0, 6.0)

    return 0;
}

3. 对称性

对于需要对称处理的运算符(如 +, == 等),友元函数提供了更好的对称性,因为它们可以对任意顺序的操作数进行操作

示例:友元函数重载 + 支持对称性
#include <iostream>

class Complex {
private:
    double real, imag;

public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 友元函数重载 + 运算符
    friend Complex operator+(const Complex& lhs, const Complex& rhs);

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

// 友元函数实现 + 运算符
Complex operator+(const Complex& lhs, const Complex& rhs) {
    return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag);
}

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2;
    Complex c4 = c2 + c1; // 同样合法
    c3.print(); // 输出: (4.0, 6.0)
    c4.print(); // 输出: (4.0, 6.0)

    return 0;
}

4. 简化实现

对于一些运算符来说,成员函数实现比友元函数实现更简洁,因为它们不需要显式地访问私有成员

示例:成员函数重载 ==
#include <iostream>

class Complex {
private:
    double real, imag;

public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 成员函数重载 == 运算符
    bool operator==(const Complex& rhs) const {
        return real == rhs.real && imag == rhs.imag;
    }

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(3.0, 4.0);
    Complex c3(1.0, 2.0);

    if (c1 == c2) {
        std::cout << "c1 is equal to c2" << std::endl; // 输出
    } else {
        std::cout << "c1 is not equal to c2" << std::endl;
    }

    if (c1 == c3) {
        std::cout << "c1 is equal to c3" << std::endl;
    } else {
        std::cout << "c1 is not equal to c3" << std::endl; // 输出
    }

    return 0;
}

友元函数的优势

友元函数可以用于解决上述成员函数重载的限制,例如支持非对称操作数和更好的对称性。

示例:友元函数重载 + 支持非类对象
#include <iostream>

class Complex {
private:
    double real, imag;

public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 友元函数重载 + 运算符
    friend Complex operator+(const Complex& lhs, const Complex& rhs);

    // 友元函数重载 Complex + double
    friend Complex operator+(const Complex& lhs, double rhs);

    // 友元函数重载 double + Complex
    friend Complex operator+(double lhs, const Complex& rhs);

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

// 友元函数实现 Complex + Complex
Complex operator+(const Complex& lhs, const Complex& rhs) {
    return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag);
}

// 友元函数实现 Complex + double
Complex operator+(const Complex& lhs, double rhs) {
    return Complex(lhs.real + rhs, lhs.imag);
}

// 友元函数实现 double + Complex
Complex operator+(double lhs, const Complex& rhs) {
    return Complex(lhs + rhs.real, rhs.imag);
}

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2;
    Complex c4 = c1 + 5.0;
    Complex c5 = 5.0 + c1;

    c3.print(); // 输出: (4.0, 6.0)
    c4.print(); // 输出: (8.0, 4.0)
    c5.print(); // 输出: (8.0, 4.0)

    return 0;
}
  • 成员函数重载:适用于一元运算符或当左操作数必须是类实例时。成员函数重载可以直接访问类的私有成员。
  • 友元函数重载:适用于需要对称性、需要访问类的私有成员以及需要支持非类对象作为操作数的运算符。友元函数可以访问类的私有和保护成员。

使用模板进行运算符重载

使用模板可以实现更加通用的运算符重载方案,适用于多种类型的相加操作。
示例:使用模板重载 + 运算符

在这里插入代码片


#include <iostream>
#include <type_traits>

class Complex {
private:
    double real, imag;

public:
    Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

    // 友元函数模板重载 + 运算符,支持 Complex + T
    template<typename T>
    friend typename std::enable_if<!std::is_same<T, Complex>::value, Complex>::type
    operator+(const Complex& lhs, const T& rhs) {
        return Complex(lhs.real + rhs, lhs.imag);
    }

    // 友元函数模板重载 + 运算符,支持 T + Complex
    template<typename T>
    friend typename std::enable_if<!std::is_same<T, Complex>::value, Complex>::type
    operator+(const T& lhs, const Complex& rhs) {
        return Complex(lhs + rhs.real, rhs.imag);
    }

    // 友元函数重载 Complex + Complex
    friend Complex operator+(const Complex& lhs, const Complex& rhs);

    void print() const {
        std::cout << "(" << real << ", " << imag << ")" << std::endl;
    }
};

// 友元函数实现 Complex + Complex
Complex operator+(const Complex& lhs, const Complex& rhs) {
    return Complex(lhs.real + rhs.real, lhs.imag + rhs.imag);
}

int main() {
    Complex c1(3.0, 4.0);
    Complex c2(1.0, 2.0);
    Complex c3 = c1 + c2;
    Complex c4 = c1 + 5.0;
    Complex c5 = 5.0 + c1;

    c3.print();  // 输出: (4.0, 6.0)
    c4.print();  // 输出: (8.0, 4.0)
    c5.print();  // 输出: (8.0, 4.0)

    return 0;
}

在这个示例中,我们使用了模板和 std::enable_if 来实现通用的运算符重载,以支持 Complex 对象与任意类型的值进行相加。通过 std::enable_if 和 std::is_same 检查,确保模板重载仅适用于非 Complex 类型
template:定义一个模板,参数类型为 T。
typename std::enable_if<!std::is_same<T, Complex>::value, Complex>::type:条件性地定义返回类型。
std::is_same<T, Complex>::value:检查类型 T 是否与 Complex 类型相同。
!std::is_same<T, Complex>::value:取反,检查类型 T 是否不等于 Complex。
std::enable_if<!std::is_same<T, Complex>::value, Complex>::type:如果 T 不是 Complex 类型,则 std::enable_if 定义了一个类型 type,该类型为 Complex。否则,模板实例化失败。

运算符重载使得自定义类型的使用更加直观和自然。通过运算符重载,可以实现与内置类型一致的语法和行为,提高代码的可读性和可维护性。在进行运算符重载时,需注意以下几点:

  1. 确保重载的运算符符合预期的行为:重载后的运算符应当符合人们对该运算符的常规预期。
  2. 避免滥用运算符重载:虽然运算符重载可以提高代码的简洁性,但不应滥用,特别是对于不适合运算符语义的操作。
  3. 遵循运算符的优先级和结合性:重载运算符的优先级和结合性与原运算符保持一致。
  • 14
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值