c++ - Calling a Derived Class Object's Method inside a Template [Using dynamic_cast] -
so i'm trying call derived class object method using dynamic_cast
template<class t> void stack<t>::objops() { t* = this->arr[top]; char s; gorilla* tempgorill = dynamic_cast<gorilla*>(a); cout << "select object functions: " << endl << endl << "<r>oar" << endl; if (tempgorill) { cout << "chest<b>eating" << endl; } else { cout << "<m>akefire" << endl << endl; } cout << "your choice-> "; cin>>(s); switch(s) { case 'r': case 'r': a->roar(); if( tempgorill) { case 'b': case 'b': a->chestbeating(); } else { case 'm': case 'm': a->makefire(); } } }
roar becasue pure virtual in monkey , chestbeating exclusivley in goriila , makefire in chimp, both derived classes of monkey. weird thing prints right methods depending choose either gorilla or chimp, cannot access methods errors get:
>c:\users\eizzy\documents\visual studio 2010\projects\copiedproject\copiedproject\newstack.h(135): error c2039: 'chestbeating' : not member of 'monkey' 1> c:\users\eizzy\documents\visual studio 2010\projects\copiedproject\copiedproject\monkey.h(8) : see declaration of 'monkey' 1> c:\users\eizzy\documents\visual studio 2010\projects\copiedproject\copiedproject\newstack.h(122) : while compiling class template member function 'void stack<t>::objops(void)' c:\users\eizzy\documents\visual studio 2010\projects\copiedproject\copiedproject\newstack.h(136): error c2039: 'makefire' : not member of 'monkey' 1> c:\users\eizzy\documents\visual studio 2010\projects\copiedproject\copiedproject\monkey.h(8) : see declaration of 'monkey'
thanks help.
in switch scope, shouldn't doing 'tempgorill->chestbeating()' ?
Comments
Post a Comment