c++ - call child static function from parent? -
c++ - call child static function from parent? -
suppose have following:
class parent { public: virtual void run() { (int = 0 ; < bar.size() ; ++it) cout << << "\n" ; }; protected: static vector<int> foo() {return vector r({1,2,3,4,5});}; static vector<int> bar; } vector<int> parent::bar = parent::foo(); now if create kid class run function called externally, how can redefine foo function homecoming else while still using parent run function?
edit: sorry allow me add together more information. suppose virtual function run() lot of code, of same. difference in parent , kid classes values want specified in vector bar, seem little wasteful redefine virtual function in kid class. however, if redefine child::bar, , phone call child::run(), parent::bar used since it's defined in parent class. there way have line "vector parent::bar = parent::foo();" know in kid class utilize "child::foo();"?
as usual. override base of operations virtual function in derived class.
class parent { public: virtual bool run() {return bar;}; static bool foo() {return true;}; static bool bar; }; class child: public parent { public: static bool foo() { homecoming false;}; }; you can still utilize base of operations version applying base:: scope resolution:
int main() { bool bc = child::foo(); bool bp = parent::foo(); std::cout << bc << bp; homecoming 0; } http://ideone.com/tdanq5
c++ inheritance
Comments
Post a Comment