Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have base class pointer and array of derived class objects. I have assigned the derived class address to base class pointer. When I incrementing base class pointer, its pointing to base class instead of derived class. So, how can access next derived class object while incrementing base class pointer?

What I have tried:

#include <iostream>

using namespace std;

class Base
{
int i;

public:
void setI(int i)
{
this->i = i;
}

int getI()
{
return i;
}
};

class Derived : public Base
{
int j;
public:
void setJ(int j)
{
this->j = j;
}

int getJ()
{
return j;
}
};

int main()
{
Base *bp;
Derived d[2];
bp = d;

d[0].setI(10);
d[1].setI(20);

cout<<bp->getI()<<" ";
bp++; //relative to base,not derived
//Pointer arithmetic is relative to the base type of the pointer. For this reason, when a base pointer is
//pointing to a derived object, incrementing the pointer does not cause it to point to the next object of derived
//type. Instead, it will point to what it thinks is the next object of the base type.
cout<<bp->getI()<
Posted
Updated 28-Dec-21 15:51pm
Comments
Richard MacCutchan 28-Dec-21 11:50am    
You cannot get a pointer to a Base object to know anything about Derived objects. If you want it to work correctly then your pointer must be of Derived type.
KarstenK 29-Dec-21 3:07am    
in real world such operations are a bad idea because it is often the cause of annoying bugs which are stopping the development of the important features. Always remember the KISS principle: "Keep it simple stupid"

Richard is correct. Pointer arithmetic requires the compiler to know the size of the objects the pointer is aimed at. The size of the derived class is the size of two integers, typically eight bytes. The size of the base class is the size of one integer, typically four bytes. This means incrementing a pointer to those objects will offset the address by different amounts so it will not work as you expect it to. To restate this, if a pointer to a derived class is incremented, the address will increase by eight bytes. Incrementing a pointer to a base class object will increase the address by four bytes. Obviously those addresses are not the same so the pointers can not be used interchangeably.
 
Share this answer
 
For what you are trying to achieve you do not need pointer arithmetic. You need a pure virtual functions
C++
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;
   }

   // cleanup important because of new
   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.
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900