簡體   English   中英

具有 object 動態數組的參數化構造函數

[英]parameterized constructor with dynamic array of object

In the below program i have used two classes, and i am trying to relate the with aggregation, i have declare class A as private in class B, and with the help of constructor i am initizing base address of class A object to private member of class B object 即(一個對象)。 我正在嘗試使用參數化構造函數將 class A 值傳遞給 class B ,但我得到的垃圾值如下:

#include <iostream>
using namespace std;

void create (B * &obj2, int siz)
{
    std::cout << obj2[0].get_nn ();     //this will run fine
    for (int i = 0; i < siz; i++)
        obj2[i] = B (10, "pranjal");    //this will also run fine

    std::cout << obj2[0].get_nn ();
}
// same line printing again, this will not give output
// *************************************** 

void display ()
{
    std::cout << object.get_data () << object.get_stringdata ();
}

// giving garbage values
// why is it happening
// *********************************program
// ************************************** enter code here
// Online C++ compiler to run C++ program online

class A {
    int rool;
    string name;
  public:
    A () { };
    A (int a, string name);
    int get_data () {
        return rool;
    }
    string get_stringdata () {
        return this->name;
    }
};

A::A (int a, string name)
{
    this->rool = a;
    this->name = name;
}

void getdetails (A * obj)
{
    for (int i = 0; i < 3; i++)
        obj[i] = A (20, "pranjal");
}

class B {

    int bbb;
    string name;
    A object;
  public:
    B () {};
    B (A s) {
        object = s;
    }
    string get_nn () {
        return object.get_stringdata ();
    }
    B (int a, string b);

    void display () {
        std::cout << object.get_data () << object.get_stringdata ();
    }
};

void dis (B * obj2)
{
    for (int i = 0; i < 2; i++) {
        obj2[i].display ();
    }
}

void create (B * &obj2, int siz)
{
    std::cout << obj2[0].get_nn ();
    for (int i = 0; i < siz; i++)
        obj2[i] = B (10, "pranjal");

    std::cout << obj2[0].get_nn () << "sd";
}

B::B (int a, string b)
{
    bbb = a;
    name = b;
}

int main ()
{
    A *obj = new A[3];
    getdetails (obj);
    
    B *obj2 = new B[3];
    
    for (int i = 0; i < 3; i++) {
        obj2[i] = B (obj[i]);
    }

    create (obj2, 3);
    dis (obj2);
    obj2->display ();

    return 0;
}
void create(B *&obj2,int siz)
{
    std::cout<<obj2[0].get_nn();      //this will run fine
    for(int i=0;i<siz;i++)
        obj2[i]=B(10,"pranjal");
    std::cout<<obj2[0].get_nn(); // same line printing again, this will not give output
}

當然不會: B(int, std::string)不會顯式調用A的構造函數,因此會調用A的默認構造函數。 這也隱式調用name的默認構造函數,這將創建一個字符串。

出於類似的原因,您會得到垃圾值:您的默認構造函數沒有為rool ,因此未初始化; 它將保持在 memory 之前的值,無論它是什么,這就是你認為的“垃圾”。

這是因為使用obj2[i] = B(10,"pranjal")創建了一個新的 object,然后將其復制到目標 object 中(經過優化,可以直接就地創建,但顯然不能依賴)。

修復A的構造函數以將值分配給rool並且問題應該 go 消失。 您可以選擇一個非零值以獲得更清晰可見的效果:

A() : rool(1977) { }

或者,您可以為 rool 提供默認值(需要 C++11):

int rool = 1977;

這會很方便,特別是如果你有更多的構造函數。

一般來說:你不應該讓你的任何對象的原始類型未初始化以避免類似的問題。

create中,您使用 2 個參數構造函數將之前的 B 對象完全替換為全新的對象......這只會使 A object 成員默認初始化。

可以做些什么來修復?

  1. 將 B 的參數傳遞給 A ctor:

     B::B (int a, string b): object(a, b) { bbb = a; name = b; }

    這樣您就可以使用與其父級相同的參數來初始化A object成員

  2. A object member重置為其先前的值:

     void create (B * &obj2, int siz) { std::cout << obj2[0].get_nn (); //this will run fine for (int i = 0; i < siz; i++) { A old = obj2[i].object; obj2[i] = B (10, "pranjal"); //this will also run fine obj2[i].object = old; } std::cout << obj2[0].get_nn (); }

    但由於它需要訪問私有成員,因此create應在B class 中聲明為friend

     class B {... friend void create (B * &obj2, int siz); };

暫無
暫無

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

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