簡體   English   中英

c++ 中的賦值運算符重載

[英]assignment operator overloading in c++

我使用以下代碼進行賦值運算符重載:

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

我的復制構造函數是這樣的:

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

在上面的運算符重載代碼中,復制構造函數被調用,因為正在創建一個新的 object; 因此我使用了以下代碼:

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();  
}

這是使用運算符重載的正確方法,現在您可以通過引用避免值復制來獲得 object。

這可能會有所幫助:

// 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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM