2011-11-18 33 views
-1

我回來了一個愚蠢的問題.. :)C++類面膜和遺產

在C++中,我想做到以下幾點:

class dataproject : public data 
{ 
    public: 
     dataproject(); 
     ~dataproject(); 

     virtual wxString GetComment(void); 

    private: 
     wxString m_comment; 
}; 

class listproject : public listdata 
{ 
    public: 
     listproject(); 
     ~listproject(); 
     bool Load();  
}; 

(one of the function of listproject) 
{ 
    dataproject data; 
    data.SetComment("blablabla"); 
    m_list.push_front(data);  
} 

class dataclient : public data 
{ 
    public: 
     dataclient(); 
     ~dataclient(); 
}; 

class listclient : public listdata 
{ 
    public: 
     listclient(); 
     ~listclient(); 
     bool Load(); 
}; 

class data 
{ 
    public: 
     data(); 
     virtual ~data(); 
     wxString GetName(void); 

    protected: 
     wxString m_name; 
}; 


class listdata 
{ 
    public: 
     data * GetById(unsigned int id); 

    protected: 
     std::list<data> m_list; 
}; 

手段,我有包含兩個類數據,然後兩個類爲每個和一個母類加載列表以列出列表類的數據。

我的解釋是相當黑紗,但我不知道該怎麼解釋呢?

如果我打電話GetName,沒問題,我得到我的數據。 如果我撥打GetComment,程序崩潰。我試圖做一些事情:

dataproject * listproject::GetById(unsigned int id) 
{ 
    return (dataproject*) listdata::GetById(id); 
} 

但它也崩潰。

我相信有一種方法可以做我想做的事,訪問子類的功能。

編輯: 爲了更清楚地說明,我將異類數據存儲在一個列表中,這些異構數據是與母類相關的兩個子類。當我回到我的列表中的一個條目時,我希望能夠訪問位於子類上的函數。

class child A : Mother 
class child B : Mother 
class Mother 

list<Mother> datalist 

datalist->function_of_child_B 
+3

我真的不明白你的問題!也許你可以發佈一個完整的可編譯示例來演示這個問題? (見http:// sscce。org /) –

+1

你的類'listclient'和'listproject'是相同的,除了name和你的類'dataclient'似乎沒用。那真的是你想要的嗎? –

+0

我截斷了代碼,因爲它太長而無法在此處發佈。這顯然是我的問題不明確,我會嘗試重新格式化它 – Damien

回答

1

而不是list<Mother>必須使用list<Mother*>。當您使用list<Mother>時,那麼列表中的項必須只是Mother,除Mother之外沒有其他內容,因爲list<>僅爲Mother對象(sizeof(Mother))提供了足夠的空間。如果嘗試在來自MotherMother的對象中「夾緊」對象,將有效地使該對象成爲Mother,但具有原始對象的虛擬表,這是未定義的行爲。當您使用list<Mother*>時,則可以從Mother獲取任何對象,因爲list<>僅爲指針(sizeof(Mother*))留出空間,並且指針的大小對於所有類都是相同的。

+0

是的,實際上使用指針不會「破壞結構」。然後可以設置課程。 – Damien

0

不能

class child A : Mother 
class child B : Mother 
class Mother 

list<Mother> datalist 

datalist->function_of_child_B 

如果我明白你的問題,爲了做到你想要什麼,你需要虛函數

class Mother{ 
public: 
    Mother(); 
    virtual function1() = 0; // It could be pure virtual 
    virtual function2() {std::cout << "Mother Function 2";}  // or just virtual 
} 

class A : public Mother { 
public: 
    function1() {std::cout << "A function 1";} 
    function2() {std::cout << "A function 2";} 
} 

class B : public Mother { 
public: 
    function1() {std::cout << "B function 1";} 
} 

現在創建一個對象,B

A* a = new A; 
B* b = new B; 

// Add them to a vector of Mother* (or list if you prefer it) 
std::vector<Mother*> v; 
v.push_back((Mother*)a); 
v.push_back((Mother*)b); 

v[0]->function1(); // prints A function 1 
v[0]->function2(); // prints A function 2 
v[1]->function1(); // prints B function 1 
v[1]->function2(); // prints Mother function 2 

現在假設類A具有名爲functionA()的函數,B具有名爲functionB()的函數,並且您想要在Mother*向量的成員v[i]中調用其中的一個。在這種情況下,你只能在你的Mother類的子類,以執行本功能使用dynamic_cast

A* pA = dynamic_cast<A*>(v[i]); 
if (pA != NULL) 
    pA->functionA(); 
B* pB = dynamic_cast<B*>(v[i]); 
if (pB != NULL) 
    pB->functionB();