Skip to content

Instantly share code, notes, and snippets.

@zehel2892
Created December 16, 2015 06:45
Show Gist options
  • Save zehel2892/d529fde5137e69b886e7 to your computer and use it in GitHub Desktop.
Save zehel2892/d529fde5137e69b886e7 to your computer and use it in GitHub Desktop.
Using pointer to base class to call functions from derived class using another independent class
#include <iostream>
#include <vector>
using namespace std;
class Base{
public:
Base(){}
~Base(){}
virtual void Write(){
cout<<"Base"<<endl;
}
};
class Derived : public Base
{
public:
Derived(){}
~Derived(){}
virtual void Write(){
cout<<"Derived"<<endl;
}
};
class Independent{
public:
Independent(){}
~Independent(){}
void DoSomething(Base * obj)
{
list.push_back(obj);
for(int a=0;a<list.size();a++)
{
list[a]->Write();
}
}
protected:
vector<Base *> list;
};
int main()
{
Independent i;
Base *d = new Derived();
i.DoSomething(d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment