2013-05-07 102 views
0

我已經寫了代碼,用於動態分配一個名稱。我知道我應該在這種情況下處理深層複製。我寫的是我自己的Copy構造函數版本,副本分配運算符和析構函數。我是否應該重新定義任何其他隱式功能,例如移動分配操作員。我不清楚移動賦值運算符的概念或任何其他隱式定義的成員函數(我已經提到的除外)。 任何人都可以添加此代碼dynName code,以顯示移動賦值運算符或任何其他隱式成員函數(如果有)。賦值運算符的對象

#include <iostream> 

using namespace std; 

class dynName{ 
    char* name; 
    int size; 
    public: 

    dynName(char* name="") 
    { 
     int n=strlen(name)+1; 
     this->name= new char[n]; 
     strncpy(this->name,name,n); 
     size=n; 
     name[size-1]='\0';//NULL terminated 
     cout << "Object created (Constructor) with name : " 
     << name << " at address " << &(this->name) << endl; 
     } 

    dynName(const dynName& Ob)//Copy Constructor 
    { 
     int n=Ob.size; 
     this->name= new char[n]; 
     strncpy(this->name,Ob.name,n); 
     size=n; 
     cout << "Object created(Copy constructor) with name : " 
     << this->name << " at address " << &(this->name) << endl; 
     } 

    //Assignment Operator 
    dynName& operator=(const dynName& ob); 

    ~dynName() 
    { 
     cout << "Object with name " << this->name << " at address " << 
     &(this->name)<<" destroyed" << endl; 
     delete[] name; 
     name=0; //Avoiding Dangling pointer if any 
     } 
    //friend ostream& operator << (ostream& os,const dynName ob); 
    //Will Call Copy Constructor 

    friend ostream& operator << (ostream& os,const dynName& ob); 
    }; 

dynName& dynName::operator=(const dynName& ob) 
{ 
    // check for self-assignment 
     if (this == &ob) 
     cout << "Created with assignment Operator " << endl; 
      return *this; 

     // first we need to deallocate any value that this string is holding! 
     delete[] this->name; 


     this->size = ob.size; 

      // this->name = new char[this->size]; 
      strncpy(this->name, ob.name,this->size); 
      cout << "Created with assignment Operator " << endl; 

    return *this; 
    } 

//ostream& operator << (ostream& os,const dynName ob) 
ostream& operator << (ostream& os,const dynName& ob) 
{ 
    os << "The name ("<< ob.size << " Letters) : " << ob.name << endl; 
    return os; 
    } 

int main() 
{ 

    dynName Ob1("Andrew Thomas"); 
    dynName Ob2; 
    dynName Ob3(Ob1); 
    dynName Ob4; 
    Ob4=Ob1;//Should Call Assignment Operator 
    cout << "\n\n\n"; 
    cout << Ob1; 
    cout << Ob2; 
    cout << Ob3; 
    cout << Ob4; 
    cout << "\n\n\n"; 

    return 0; 
    } 

這段代碼的問題是,它不叫我的拷貝賦值運算符。任何幫助,爲何如此?

$ ./Trial

Object created (Constructor) with name : Andrew Thomas at address 0x22ac40 
Object created (Constructor) with name : at address 0x22ac30 
Object created(Copy constructor) with name : Andrew Thomas at address 0x22ac20 
Object created (Constructor) with name : at address 0x22ac10 



The name (14 Letters) : Andrew Thomas 
The name (1 Letters) : 
The name (14 Letters) : Andrew Thomas 
The name (1 Letters) : 



Object with name at address 0x22ac10 destroyed 
Object with name Andrew Thomas at address 0x22ac20 destroyed 
Object with name at address 0x22ac30 destroyed 
Object with name Andrew Thomas at address 0x22ac40 destroyed 

感謝

編輯

參考Move assignment operator and `if (this != &rhs)` 什麼是Class&&?我的意思是我從來沒有使用過這種東西..只是引用,即Class&

+0

您發佈崩潰,我的代碼。 – jrok 2013-05-07 12:28:18

+3

不要重新創建另一個斷開的字符串類,使用std :: string字段並讓它處理內存管理。 – 2013-05-07 12:28:26

+0

我已經從您的標題中刪除了「動態」一詞,因爲它在這裏無關緊要。在你的'int main'中,所有'ObX'對象都是**不動態**。他們內在的內容是,但對象不是。 – quetzalcoatl 2013-05-07 12:29:52

回答

5

看來你缺少括號在這裏:

if (this == &ob) 
    cout << "Created with assignment Operator " << endl; 
     return *this; 

只有輸出是if身體的一部分,return語句總是被執行。

4

應該調用拷貝操作,但你總是自我分配檢查後返回。

dynName& dynName::operator=(const dynName& ob) 
{ 
    // check for self-assignment 
     if (this == &ob) 
     cout << "Created with assignment Operator " << endl; 
      return *this; //THIS LINE is not in the if block 

     // first we need to deallocate any value that this string is holding! 
     delete[] this->name; 


     this->size = ob.size; 

      // this->name = new char[this->size]; 
      strncpy(this->name, ob.name,this->size); 
      cout << "Created with assignment Operator " << endl; 

    return *this; 
    } 
+0

注入代碼中的註釋容易被忽略。你已經在答案的文本中詳細闡述了它。 – quetzalcoatl 2013-05-07 12:34:10