C++ 中的赋值运算符重载

assignment operator overloading in c++

本文关键字:重载 赋值运算符 C++      更新时间:2023-10-16

我使用以下代码进行赋值运算符重载:

SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs)
{
     if(this == &rhs)
        return *this;
     itsRadius = rhs.getRadius();
     return *this;
}

我的复制构造函数是这样的:

SimpleCircle::SimpleCircle(const SimpleCircle & rhs)
{
    itsRadius = rhs.getRadius();
}

在上面的运算符重载代码中,由于正在创建一个新对象,因此调用了复制构造函数;因此我使用了以下代码:

SimpleCircle & SimpleCircle::operator=(const SimpleCircle & rhs)
{
    if(this == &rhs)
       return *this;
    itsRadius = rhs.getRadius();
    return *this;
}

它运行良好,避免了复制构造函数问题,但是(对我来说)是否有任何未知问题?

第二个版本的赋值运算符没有问题。事实上,这是赋值运算符的标准方法。

编辑:请注意,我指的是赋值运算符的返回类型,而不是实现本身。正如评论中指出的那样,执行本身是另一个问题。看这里。

第二个非常标准。您通常更喜欢从赋值运算符返回引用,以便像 a = b = c; 这样的语句按预期解析。我想不出任何我想从作业中返回副本的情况。

需要注意的一点是,如果您不需要深层副本,有时认为最好使用编译器生成的隐式复制构造函数和赋值运算符,而不是自己创建。不过真的取决于你...

编辑:

以下是一些基本调用:

SimpleCircle x; // default constructor
SimpleCircle y(x); // copy constructor
x = y; // assignment operator

现在假设我们有您的赋值运算符的第一个版本:

SimpleCircle SimpleCircle::operator=(const SimpleCircle & rhs)
{
     if(this == &rhs)
        return *this; // calls copy constructor SimpleCircle(*this)
     itsRadius = rhs.getRadius(); // copy member
     return *this; // calls copy constructor
}

它调用复制构造函数并将引用传递给this,以便构造要返回的副本。现在在第二个示例中,我们通过仅返回对this的引用来避免复制

SimpleCircle & SimpleCircle::operator=(const SimpleCircle & rhs)
{
    if(this == &rhs)
       return *this; // return reference to this (no copy)
    itsRadius = rhs.getRadius(); // copy member
    return *this; // return reference to this (no copy)
}

在这种情况下,您几乎可以肯定最好跳过对自我分配的检查 - 当您只分配一个看似简单类型(可能是双精度)的成员时,执行该分配通常比避免它更快,因此您最终会得到:

SimpleCircle & SimpleCircle::operator=(const SimpleCircle & rhs)
{
    itsRadius = rhs.getRadius(); // or just `itsRadius = rhs.itsRadius;`
    return *this;
}

我意识到许多较旧和/或质量较低的书籍建议检查自我分配。然而,至少根据我的经验,没有它你会过得更好(如果操作员依赖它的正确性,它几乎肯定不是例外安全的)。

顺便说一句,我要指出的是,要定义一个圆,你通常需要一个中心和一个半径,当你复制或分配时,你想要复制/分配两者。

#include<iostream>
using namespace std;
class employee
{
    int idnum;
    double salary;
    public:
        employee(){}
        employee(int a,int b)
        {
            idnum=a;
            salary=b;
        }
        void dis()
        {
            cout<<"1st emp:"<<endl<<"idnum="<<idnum<<endl<<"salary="<<salary<<endl<<endl;
        }
        void operator=(employee &emp)
        {
            idnum=emp.idnum;
            salary=emp.salary;
        }
        void show()
        {
            cout<<"2nd emp:"<<endl<<"idnum="<<idnum<<endl<<"salary="<<salary<<endl;
        }
};
main()
{
    int a;
    double b;
    cout<<"enter id num and salary"<<endl;
    cin>>a>>b;
    employee e1(a,b);
    e1.dis();
    employee e2;
    e2=e1;
    e2.show();  
}
这是

使用运算符重载的正确方法现在,您可以通过引用获取对象避免值复制。

这可能会

有所帮助:

// Operator overloading in C++
//assignment operator overloading
#include<iostream>
using namespace std;
class Employee
{
private:
int idNum;
double salary;
public:
Employee ( ) {
    idNum = 0, salary = 0.0;
}
void setValues (int a, int b);
void operator= (Employee &emp );
};
void Employee::setValues ( int idN , int sal )
{
salary = sal; idNum = idN;
}
void Employee::operator = (Employee &emp)  // Assignment operator overloading function
{
salary = emp.salary;
}
int main ( )
{
Employee emp1;
emp1.setValues(10,33);
Employee emp2;
emp2 = emp1; // emp2 is calling object using assignment operator
}