class Hi
{
public:
Hi(){};
void test() {cout << "test" << endl;}
void test2(){cout << "Hi!" << endl;}
};
class Hi2 : public Hi
{
public:
void test() {};
};
int main ()
{
Hi h;
Hi2 h2;
cout <<"testing Hi\n";
h.test();
h.test2();
cout <<"testing Hi2\n";
h2.test();
h2.test2();
cout << "casting \n";
((Hi2*)(&h))->test(); //makes an object of Hi class type use the empty override!
}
if you are trying to fix a large amount of code that already uses the object so that sometimes it has an alternate method... it depends. If you are allowed to tamper with the original object, perhaps there is a simpler way:
1 2 3 4 5 6 7 8 9
void test(bool b = true)
{
if(b)
{
//original body
}
//implied else do nothing
}
..
that makes the parameter optional for the previous/existing code:
thing.test();//will do the original code
newthing.test(false);//will not do the original code
if you cannot or should not (eg, 3rd party) tamper with it, derived solution will work and your new code or modified code can cast or replace.