有沒有什麼辦法可以做以下工作,或者是否有解決方法?我肯定錯過了什麼。C++多態 - 使用基址指針訪問派生函數
class base
{
public:
int someInt;
virtual void someFunction(){}
};
class derived : public base
{
public:
void someFunction(){}
void anotherFunction(){}
};
int main (int argc, char * const argv[]) {
base* aBasePointer = new derived;
aBasePointer->anotherFunction();
delete aBasePointer
return 0;
}
您需要'anotherFunction()'以便在基礎上虛擬。或者通過指向派生的指針來調用它。目前基地對此功能一無所知。 – BoBTFish 2012-03-13 17:08:59
* real *函數是什麼?你爲什麼想這樣做? – 2012-03-13 17:18:55
它也需要在Base中被虛擬和定義。如果您將新的虛擬功能添加到派生,它並沒有幫助。 – 2012-03-13 19:52:03