0
我在派生指向派生類的指針時遇到了一些麻煩。我認爲這與構造函數有關。我必須在派生類中創建一個新的構造函數嗎?如何從派生類創建指針?謝謝指向派生類C++的指針
Class Base
{
public:
int myfunction(int a,int b,int c)
{
return a+b+c;
}
};
Class Derived: public Base
{
int newfunction(int a, int b, int c)
{
return a*b*c;
};
};
int main()
{
// this doesn't work at all.. I get all errors every time I try to refer to the object
//instantiated from my derived class.
//I know it's my lack of understanding.
Derived *NewObject = new Derived;
//Why wont this work?
}
這只是示例中的一個錯字。我無法粘貼實際的代碼,因爲我實際上是從一些開源軟件(FLTK)派生出一個類,我似乎無法從中聲明一個新類。可能有太多的代碼,會卷積這個過程。 – user3523966
你可以在基類中做到這一點嗎? –
@ user3523966:我敢打賭,問題在於分配對象,而不是分配給指針。只是'新派生'工作?如果沒有,那麼你應該給我們更多的信息,比如編譯器顯示的錯誤。 – peoro