运算符重载详解

一般运算符重载

在进行对象之间的运算时,程序会调用与运算符相对应的函数进行处理,所以运算符重载有两种方式:成员函数和友元函数。成员函数的形式比较简单,就是在类里面定义了一个与操作符相关的函数。友元函数因为没有this指针,所以形参会多一个。

// 运算符重载,这里又叫赋值函数
string& operator =(const string &other);
// 运算符重载,但这里要用友元函数才行
friend ostream& operator << (ostream &os,const string &str);

string &string::operator=(const string &other)
{
    if(this == &other)
        return *this;
    delete []m_data;
    m_data = NULL;
    m_data = new char[strlen(other.m_data)+1];
    strcpy(m_data,other.m_data);
    return *this;
}

ostream& operator << (ostream &os,const string& str)    //  返回引用用于实现链式表达式
{
    os<<str.m_data;
    return os;
}

成员函数重载与友元函数重载的区别:

If you define your operator overloaded function as member function, then compiler translates expressions like s1 + s2 into s1.operator+(s2).That means, the operator overloaded member function gets invoked on the first operand. That is how member functions work!

But what if the first operand is not a class? There's a major problem if we want to overload an operator where the first operand is not a class type, rather say double. So you cannot write like this 10.0 + s2. However, you can write operator overloaded member function for expressions like s1 + 10.0.

To solve this ordering problem, we define operator overloaded function as friend IF it needs to access private members. Make it friend ONLY when it needs to access private members. Otherwise simply make it non-friend non-member function to improve encapsulation!

class Complex //复数类
{
private://私有
double real;//实数
double imag;//虚数
public:
        Complex(double real=0,double imag=0)
        {
this->real=real;
this->imag=imag;
        }
        Complex operator+(int x);
};

Complex Complex::operator+(int x)
{
return Complex(real+x,imag);
}

int main()
{
    Complex com1(5,10),total;
    total=com1+5;

return0;
}

把上述main()主函数实现部分里的total=com1+5改为total=5+com1;那么程序就会报错(没有与这些操作数匹配的 "+" 运算符),因为左操作数5不是该复数类的对象,不能调用相应的成员函数Complex operator+(int x),所以编译错误。但如果我们定义一下两个友元函数就能解决上述的问题:

friend Complex operator+(Complex com1,int x);

friend Complex operator+(int x,Complex com1);

两种重载方式的比较:

  • 一般情况下,单目运算符最好重载为类的成员函数;双目运算符则最好重载为类的友元函数。
  • 以下一些双目运算符不能重载为类的友元函数:=、()、[]、->。
  • 类型转换函数只能定义为一个类的成员函数而不能定义为类的友元函数。 C++提供4个类型转换函数:reinterpret_cast(在编译期间实现转换)、const_cast(在编译期间实现转换)、stactic_cast(在编译期间实现转换)、dynamic_cast(在运行期间实现转换,并可以返回转换成功与否的标志)。
  • 若一个运算符的操作需要修改对象的状态,选择重载为成员函数较好。
  • 若运算符所需的操作数(尤其是第一个操作数)希望有隐式类型转换,则只能选用友元函数。
  • 当运算符函数是一个成员函数时,最左边的操作数(或者只有最左边的操作数)必须是运算符类的一个类对象(或者是对该类对象的引用)。如果左边的操作数必须是一个不同类的对象,或者是一个内部 类型的对象,该运算符函数必须作为一个友元函数来实现。(比如流操作符,即<<,>>)
  • 当需要重载运算符具有可交换性时,选择重载为友元函数。

注意事项:

  1. 除了类属关系运算符"."、成员指针运算符".*"、作用域运算符"::"、sizeof运算符和三目运算符"?:"以外,C++中的所有运算符都可以重载。
  2. 重载运算符限制在C++语言中已有的运算符范围内的允许重载的运算符之中,不能创建新的运算符。
  3. 运算符重载实质上是函数重载,因此编译程序对运算符重载的选择,遵循函数重载的选择原则。
  4. 重载之后的运算符不能改变运算符的优先级和结合性,也不能改变运算符操作数的个数及语法结构。
  5. 运算符重载不能改变该运算符用于内部类型对象的含义。它只能和用户自定义类型的对象一起使用,或者用于用户自定义类型的对象和内部类型的对象混合使用时。
  6. 运算符重载是针对新类型数据的实际需要对原有运算符进行的适当的改造,重载的功能应当与原有功能相类似,避免没有目的地使用重载运算符。

原文链接: https://www.cnblogs.com/balingybj/p/4772843.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/221369

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午11:15
下一篇 2023年2月13日 上午11:15

相关推荐