簡體   English   中英

創建不同對象的數組

[英]Creating array of different objects

我有這段代碼,但是在這里我看不到哪里出錯了。 它似乎可以編譯,但是我無法訪問ComputerAppliance功能。 有人可以幫我理解如何在我在此代碼示例上創建一個包含不同對象的數組嗎?

#include <iostream>
using namespace std;

class Technics
{
private:
    int price, warranty;
    static int objCount;
    double pvn;
    char *name, *manufacturer;
public:
    Technics()
    {
        this->objCount++;
    };

    Technics(int price)
    {
        this->objCount++;
        this->price = price;
    }

    ~Technics(){
        this->objCount = this->objCount - 2;
    };

    static int getObjCount()
    {
        return objCount;
    }

    void setPrice(int price)
    {
        this->price = price;
    }

    int getPrice()
    {
        return this->price;
    }

    void resetCount()
    {
        this->objCount = 0;
    }
};
int Technics::objCount = 0;

class Computer : public Technics
{
private:
    int cpu, ram, psu, hdd;
public:
    Computer() {}
    Computer(int price)
    {
        this->setPrice(price);
    }

    void setCpu(int cpu)
    {
        this->cpu = cpu;
    }

    int getCpu()
    {
        return this->cpu;
    }
};

class Appliance : public Technics
{
private:
    int height;
    int width;
    char* color;
    char* type;

public:
    Appliance(){}
    Appliance(int height, int width)
    {
        this->height = height;
        this->width = width;
    }

    void setWidth(int width)
    {
        this->width = width;
    }

    int getWidth()
    {
        return this->width;
    }
};

void main()
{
    //Creating array
    Technics *_t[100];

    // Adding some objects
    _t[0] = new Computer();
    _t[1] = new Computer();
    _t[2] = new Appliance();

    // I can access only properties of Technics, not Computer or Appliance
    _t[0]->

    int x;
    cin >> x;
}

該行:

_t[0] = new Computer();

創建一個計算機對象,並將其作為Technics基本指針存儲在數組中(即,出於所有意圖和目的,在該數組中,它是一個Technics對象)。

您需要回退到派生類以訪問比Technics中派生的成員更多的成員:

static_cast<Computer*>(_t[0])->Your_Member();

如果您不知道它是哪個派生類型,請使用動態轉換-成功時將返回轉換指針,失敗時將返回NULL,因此有點類型檢查-運行時開銷很大,因此請避免使用它:)

根據您的結束評論進行編輯

//Calculate the length of your dynamic array.

//Allocate the dynamic array as a pointer to a pointer to Technics - this is like
//Making an array of pointers each holding some Technics heirarchy object.
Technics** baselist = new Technics*[some_length];

//Populate them the same way as before:
baselist[0] = new Computer();
baselist[1] = new Appliance();

PS:您還可以使用std :: vector,它可以動態更改,而不是在運行時創建-如果允許使用,這是最佳選擇。 它可以節省您制作自己可調整大小的數組代碼的時間。 谷歌一下 ;)

這是因為_t是指向Technics的指針,而不是ComputerAppliance的指針。

給工藝“對象類型”參數如枚舉是TechnicsType.ComputerComputerTechnicsType.ApplicanceAppliance ,檢查並轉換為相應的類型,以獲得類的方法。

是的,您只能訪問Technics的屬性,因為您的變量的類型為Technics 您必須將其強制轉換為Computer或Appliance類,才能執行其他方法。

您確實必須在這里考慮您的設計。 真的合適嗎? 為什么將所有對象都放在同一個容器中? 特別是如果您使用不同的方法來調用..這沒有意義。

如果您確實要調用其他方法,則可能必須使用switch語句來確定您擁有的類型,然后調用適當的方法(我想您想遍歷整個容器,否則對有一個裝有不同物體的大容器)。

解決方案非常非常簡單:)

超類必須具有在類定義中聲明的子類的虛函數。

例如:如果超類計算機具有一個子類被稱為筆記本電腦 ,具有函數int getBatteryLife(); ,因此計算機類必須在類型計算機的指針向量中具有定義virtual int getBatteryLife()的定義。

由於_tTechnics指針數組,因此無法訪問派生類的屬性。 像這樣使用訪客模式或向下移動指針:

// visitor pattern
class Visitor
{
    void accept(Appliance &ref) { // access Appliance attributes };
    void accept(Computer & ref) { // access Computer attributes };
};

class Technics
{
    ....
    virtual void visit(Visitor &) = 0;
};

class Appliance
{
    ....
    virtual void visit(Visitor &v) { v.accept(*this); }
};

class Computer
{
    ....
    virtual void visit(Visitor &v) { v.accept(*this); }
};

暫無
暫無

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

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