簡體   English   中英

在矢量中存儲各種類型

[英]Storing various types in a vector

我試圖在數組或向量中存儲各種不同類型的數據。 到目前為止,我通過使用一個基類來實現這一點,該基類將作為指向每個對象的指針存儲在向量中,然后鍵入強制轉換以獲取數據。 這適用於int,但任何其他類型的數據都會引發訪問沖突異常。

對不起,如果我的解釋不是很好,這是我的代碼和評論,我希望將有所幫助:

//Base class
class MenuProperty
{
private:
    std::string Name;

public:
    MenuProperty(std::string Name) : Name(Name) {};
    ~MenuProperty() {};

    std::string GetName();

};

//Typed class used to store data
template<class T>
class TMenuProperty : public MenuProperty
{
private:
    T Data;

public:
    TMenuProperty(std::string Name, T Data) : MenuProperty(Name), Data(Data) {};

    T GetData()
    {
       return this->Data;
    }
};

//Class with no type and data pointer to retrieve data
class cpMenuProperty : public MenuProperty
{
private:
    VOID* Data;

public:
    cpMenuProperty(std::string Name) : MenuProperty(Name) {};

    VOID* GetPointer()
    {
       return this->Data;
    }
};

希望有一些相似之處,這是我的測試代碼:

int main()
{
    TMenuProperty<double> fP("Test2", 33.7354); //Create instance of property

    MenuProperty* fMP = &fP;                    //Make a pointer to the object

    cpMenuProperty* Test;                       //Make a pointer to the retrieving
                                                //object

    std::vector<MenuProperty*>              Vec;
    std::vector<MenuProperty*>::iterator    it;

    Vec.push_back(fMP);                         

    it = Vec.begin();

    Test = static_cast<cpMenuProperty*>(*it);   //Cast the first object in the list 
                                                //list to the same type as the
                                                //retrieveing object


    double Data = *(double*)Test->GetPointer(); //Dereference and access, this is
                                                //where the exception is thrown

    std::cout << Data;



    int Ret;
    std::cin >> Ret;
}

我可能在這里犯了一些巨大的錯誤,但是感謝你花時間閱讀它到目前為止:)任何幫助都是值得贊賞的,也是建設性的批評!

您正在初始化堆棧上的TMenuProperty對象,然后將其轉換為cpMenuProperty。 永遠不會為cpMenuProperty中的void * Data分配任何內存。 TMenuProperty和cpMenuProperty之間沒有任何關系,除了它們來自同一個類。 這種設計永遠不會起作用。

  • 擺脫所有的空虛*。 那是在惹麻煩。
  • 除非你100%知道你在做什么,否則不要使用static_cast <>。 使用dynamic_cast,它會告訴你轉換是無效的(我猜你試過這個,但后來回到static_cast強制代碼至少編譯:))
  • 你為什么不一直使用TMenuProperty? 這種方法應該有效。
  • 對於你正在做的事情的其他方式,請看boost :: variant和boost :: any。
  • 如果你是勇敢的並且真的知道你在做什么(沒有冒犯,但我認為你沒有資格獲得這個),如果你需要包裝的數據類型在內存布局方面足夠統一,你可以使您的代碼使用適當的內存填充設置並以某種方式強制進行內存對齊。 但是,無論多么不可能,我都無法想出任何理由來找到這樣的理由。 所以從我在這篇文章中可以看出,我只能建議刪除cpMenuProperty並僅使用抽象基類/模板化派生類方法。
 #include<iostream>
 #include<vector>
 #include<iterator>
 #include<memory>
 class base {
 public:
 virtual void foo(){
      std::cout << "in base" << std::endl;
  }
 };

 class derived : public base {
 public:
 virtual void foo(){
      std::cout << "in derived" << std::endl;
   }
 };

 int main()
 {
    std::vector<std::unique_ptr<base>> vec;
    vec.emplace_back(new derived);
    static_cast<derived*>(vec[0].get())->foo();
    return 0;
  }

經典的例子,使用現代實踐。

暫無
暫無

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

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