For what you are trying to achieve you do not need pointer arithmetic. You need a pure virtual functions
class Base
{
protected:
int i;
public:
virtual void set(int i)=0;
virtual int get() = 0;
};
class Derived : public Base
{
public:
virtual void set(int i)
{
this->i = i;
}
virtual int get()
{
return i;
}
};
int main()
{
std::vector<Base*> vec;
vec.push_back(new Derived);
vec.push_back(new Derived);
vec[0]->set(10);
vec[1]->set(20);
for(auto p : vec)
{
std::cout << p->get() << std::endl;
}
for(auto p : vec)
{
delete p;
}
return 0;
}
You can manipulate any object via base pointer by using virtual functions.
So for example if you have more than one class derived from the Base, they all can be stored in an array as a base pointer and accessed through base pointer virtual functions - Interface to be exact. And you want the base class contain pure vitual interface so that the derived classes are forced by the compiler to write implementation of that interface.