程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++讀書筆記之重載賦值運算符 overload assignment operator

C++讀書筆記之重載賦值運算符 overload assignment operator

編輯:C++入門知識

[cpp] view plaincopyprint?
#include <iostream>  
#include <cstring>  
using namespace std; 
 
class Internet 

    public: 
    Internet(char *name,char *url) 
    { 
        this->name = new char[strlen(name)+1]; 
        this->url = new char[strlen(url)+1]; 
        if(name!=NULL) 
        { 
            strcpy(this->name,name); 
        } 
        if(url!=NULL) 
        { 
            strcpy(this->url,url); 
        } 
    } 
    Internet(Internet &temp) 
    { 
        this->name=new char[strlen(temp.name)+1]; 
        this->url=new char[strlen(temp.url)+1]; 
        if(this->name) 
        { 
            strcpy(this->name,temp.name); 
        } 
        if(this->url) 
        { 
            strcpy(this->url,temp.url); 
        } 
    } 
    ~Internet() 
    { 
        delete[] this->name; 
        delete[] this->url; 
    } 
    Internet& operator =(Internet &temp)//賦值運算符重載函數  
    { 
        delete[] this->name; 
        delete[] this->url; 
        this->name = new char[strlen(temp.name)+1]; 
        this->url = new char[strlen(temp.url)+1]; 
        if(this->name!=NULL) 
        { 
            strcpy(this->name,temp.name); 
        } 
        if(this->url!=NULL) 
        { 
            strcpy(this->url,temp.url); 
        } 
        return *this; 
    } 
    public: 
        char *name; 
        char *url; 
}; 
int main() 

    Internet a("王世晖的專欄","blog.csdn.net/shihui512"); 
    Internet b = a;//b對象還不存在,所以調用拷貝構造函數,進行構造處理。  
    cout<<b.name<<endl<<b.url<<endl; 
    Internet c("清水河畔外網","bbs.qshpan.com"); 
    b = c;//b對象已經存在,所以系統選擇賦值運算符重載函數處理。  
    cout<<b.name<<endl<<b.url<<endl; 

/*****************************
運行結果:
王世晖的專欄
blog.csdn.net/shihui512
清水河畔外網
bbs.qshpan.com
 
******************************/ 

#include <iostream>
#include <cstring>
using namespace std;

class Internet
{
    public:
    Internet(char *name,char *url)
    {
        this->name = new char[strlen(name)+1];
        this->url = new char[strlen(url)+1];
        if(name!=NULL)
        {
            strcpy(this->name,name);
        }
        if(url!=NULL)
        {
            strcpy(this->url,url);
        }
    }
    Internet(Internet &temp)
    {
        this->name=new char[strlen(temp.name)+1];
        this->url=new char[strlen(temp.url)+1];
        if(this->name)
        {
            strcpy(this->name,temp.name);
        }
        if(this->url)
        {
            strcpy(this->url,temp.url);
        }
    }
    ~Internet()
    {
        delete[] this->name;
        delete[] this->url;
    }
    Internet& operator =(Internet &temp)//賦值運算符重載函數
    {
        delete[] this->name;
        delete[] this->url;
        this->name = new char[strlen(temp.name)+1];
        this->url = new char[strlen(temp.url)+1];
        if(this->name!=NULL)
        {
            strcpy(this->name,temp.name);
        }
        if(this->url!=NULL)
        {
            strcpy(this->url,temp.url);
        }
        return *this;
    }
    public:
        char *name;
        char *url;
};
int main()
{
    Internet a("王世晖的專欄","blog.csdn.net/shihui512");
    Internet b = a;//b對象還不存在,所以調用拷貝構造函數,進行構造處理。
    cout<<b.name<<endl<<b.url<<endl;
    Internet c("清水河畔外網","bbs.qshpan.com");
    b = c;//b對象已經存在,所以系統選擇賦值運算符重載函數處理。
    cout<<b.name<<endl<<b.url<<endl;
}
/*****************************
運行結果:
王世晖的專欄
blog.csdn.net/shihui512
清水河畔外網
bbs.qshpan.com

******************************/

 

 


the correct and safe version of the MyClass assignment operator would be this:

  MyClass& MyClass::operator=(const MyClass &rhs) {
    // Check for self-assignment!
    if (this == &rhs)      // Same object?
      return *this;        // Yes, so skip assignment, and just return *this.

    ... // Deallocate, allocate new space, copy values...

    return *this;
  }Or, you can simplify this a bit by doing:
  MyClass& MyClass::operator=(const MyClass &rhs) {

    // Only do assignment if RHS is a different object from this.
    if (this != &rhs) {
      ... // Deallocate, allocate new space, copy values...
    }

    return *this;
  }Remember that in the comparison, this is a pointer to the object being called, and &rhs is a pointer to the object being passed in as the argument. So, you can see that we avoid the dangers of self-assignment with this check.

In summary, the guidelines for the assignment operator are:

Take a const-reference for the argument (the right-hand side of the assignment).
Return a reference to the left-hand side, to support safe and reasonable operator chaining. (Do this by returning *this.)
Check for self-assignment, by comparing the pointers (this to &rhs).

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved