C++ Calling a derived class function from the base class -
i'm not sure how call derived class function base class without getting sort of error, here's skeleton of code i'm trying work with...
class dicegame{ public: virtual void play(){ // trying call derived class // knockout's play function in here // i've been trying dicegame *game = new knockout; game->play(); // gives me memory error } class knockout : public dicegame{ void play(){ // has stuff want call } main(){ dicegame *game = new dicegame(); game->play(); } i have tried forward declaration of knockout class, gives me incomplete forward declaration error well, advice?
class dicegame{ public: virtual void play() = 0; }; class knockout : public dicegame{ public: virtual void play() { // replace "virtual" "override" here if compiler supports "override" keyword. // has stuff want call } }; int main() { dicegame *game = new knockout(); game->play(); return 0; }
Comments
Post a Comment