assigning derived class pointer to base class pointer in C++
c++
Solution
If you are adamant that this function should NOT be a part of base, you have but 2 options to do it.
Either use a pointer to derived class
derived* pDerived = new derived();
pDerived->myFunc();
Or (uglier & vehemently discouraged) static_cast the pointer up to derived class type and then call the function NOTE: To be used with caution. Only use when you are SURE of the type of the pointer you are casting, i.e. you are sure that `pbase` is a `derived` or a type derived from `derived`. In this particular case its ok, but im guessing this is only an example of the actual code.
base* pbase = new derived();
static_cast<derived*>(pbase)->myFunc();
Problem
I have following ``` class base { }; class derived : public base { public: derived() {} void myFunc() { cout << "My derived function" << std::endl; } }; ``` now I have ``` base* pbase = new derived(); pbase->myFunc(); ``` I am getting error myFunc is not a member function of base. How to avoid this? and how to make myFunc get called? Note I should have base class contain no function as it is part of design and above code is part of big function