c++ - How do I call an overloaded function from a function in the base class? -
i have class (b) inherits class (a). want call function class has been overridden. want able call overridden function independent of class inherited base (say class c : public a
, want call c's version of function.)
here's example
class { public: void callf(); virtual void f() {}; }; class b : public { public: void f(); }; void a::callf() { //fyi, want able call without knowing super class is. f(); } void b::f() { std::cout << "i want function called, instead default f() called."; }
edit: in real code, have std::vector<a> avector;
. call avector.push_back(b());
. if called avector[0].callf();
, default a::f()
called. answered below, have problem slicing.
your construction:
vector_of_a.push_back( b() );
doesn't store b
in vector. constructs b
, constructs a
that, stores that a
in vector. consequence, experience slicing.
see more info:
Comments
Post a Comment