簡體   English   中英

如果 c++ 中沒有賦值運算符,轉換構造函數是否會創建 object 並銷毀它?

[英]Does conversion constructor create an object and destroys it if there is no assignment operator in c++?

我有一個關於轉換構造函數和賦值運算符的基本問題。 我找不到類似的問題,但也許我搜索錯誤。 無論如何..我做了一個像這樣的class

class String
{
private:
//    enum { SZ = 80 };
    static const int SZ = 80;
    char str[SZ]; //array
public:
    String() //constructor, no args
    {
        cout << "Default constructor called, p_str = " << (void*)str << endl;
        strcpy(str, "");
    }
    String( char s[] ) //constructor, one arg
    {
        cout << "Copy constructor called, p_str = " << (void*)str << endl;
        strcpy(str, s);
    }
    void display() //display string
    {
        cout << str << endl;
//        cout << "str ptr = " << (void*)str << endl;
    }
    void concat(String s2) //add arg string to
    { //this string
        if( strlen(str)+strlen(s2.str) < SZ )
            strcat(str, s2.str);
        else
            cout << "\nString too long";
    }
    void SetString(char* strToSet)
    {
        strcpy(str, strToSet);
    }
//    void operator =(const char* strCpy)
//    {
//        cout << "Copy assignemnt called..." << endl;
//        strcpy(str, strCpy);
//    }
    ~String()
    {
        cout << "Destructor called..." << endl;
    }
    void* GetStrPtr()
    {
        return (void*)str;
    }
};

主要是:

    String myStr1 = "Hello Hello";

    void* old_str_ptr = myStr1.GetStrPtr();
    cout << "old_str_ptr = " <<old_str_ptr << endl;

    myStr1 = "hello World!!";
    cout << "old_str_ptr = " <<old_str_ptr << endl;
    void* new_str_ptr = myStr1.GetStrPtr();
    cout << "new_str_ptr = " <<new_str_ptr << endl;
    myStr1.display();
    cout << (char*)old_str_ptr << endl;
    cout << (char*)new_str_ptr << endl;

這是我得到的 output:

Copy constructor called, p_str = 0x62fdd8
old_str_ptr = 0x62fdd8
Copy constructor called, p_str = 0x62fe28
Destructor called...
old_str_ptr = 0x62fdd8
new_str_ptr = 0x62fdd8
hello World!!
hello World!!
hello World!!
Destructor called...

有人可以解釋一下 main 中這一行到底發生了什么:

myStr1 = "hello World!!"

正如我所看到的,它調用了轉換構造函數(因為賦值運算符被注釋)並且“str”數組的地址發生了變化,那么我不明白的是調用了析構函數並返回了地址,如中所示output。

myStr1 = "hello World;!"; 這兩種類型不兼容,因此通常無法進行分配。 但是,編譯器注意到您有一個接受指向char的指針的隱式轉換構造函數,因此它調用此構造函數來創建一個臨時的 object,可以從中進行賦值。 這是事件的順序:

  • 一個臨時的String object 被構造,調用String("hello World!"")
  • 臨時值是復制分配 (C++ < 11) 或移動分配 (C++ >= 11) 到myStr1 (您不會重載分配,因此您不會在輸出中觀察此步驟)。 無論是復制分配還是移動分配,在這種情況下,相關的編譯器生成的賦值運算符都會執行簡單的成員值復制,因為無法移動成員。
  • 臨時的被破壞了。

str的位置不會僅僅因為它不能改變:它是 class 的數組成員,這意味着它的存儲直接作為String對象的一部分分配。 str被評估為指向String objectmemory 區域的指針。

隱式移動賦值運算符只是將源對象的str數組的內容復制到目標對象的str數組中。

您會在第二條構造函數消息中看到不同的值,因為這是正在構造的不同 object,因此其str成員存在於不同的 memory 位置。 但是,這個 object 在其值被復制到myStr1 object 后被破壞。

暫無
暫無

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

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