Derived class pointer increment

I am writing a program for a pointer to a Derived class. Here is my code

#include <iostream> using namespace std; class base { int i; public: void set_i(int num) { i=num; } int get_i() { return i; } }; class derived: public base { int j; public: void set_j(int num) {j=num;} int get_j() {return j;} }; int main() { base *bp; derived d[2]; bp = d; d[0].set_i(1); d[1].set_i(2); cout << bp->get_i() << " "; bp++; cout << bp->get_i(); return 0; } 

The program displays 1 correct value and another garbage value, because

by. ++;

increases the pointer to the next object of the base class type , but not to the derived class type .

we can correctly display the answer by writing

 > bp =& d[0]; bp1=&d[1]; d[0].set_i(1); d[1].set_i(2); 

but then we need to assign 2 pointers. Similarly, if we need to accept 100 values, we need to assign 100 pointers. This is not good.

My question is, can we show the value of an array with a single pointer?

+4
source share
5 answers

I assume that the reason the OP is trying to do this is because it can have an array of objects that can be arbitrary derived instances of some base class and iterate over them. If so, you really need an array of pointers to your base class. Sort of:

 class Base { /* ... */ }; class Derived1 : public Base { /* ... */ }; class Derived2 : public Base { /* ... */ }; // ... Base *arr[10] = {new Derived1(), new Derived1(), new Derived2(), ...}; // ... for(Base **p = arr; p < arr+10; ++p) { *p->foo(); // ... } 

If I had guessed about the real OP problem, I think that would solve it.

+5
source

You cannot do what you are trying to do.

You create an array of objects that have a derived size. When you use a base pointer to an array, you are trying to move the pointer to the size of the base. If you have an array of Derived *, you can point to them with the base * *, and everything will work. If you have real objects, you must use the correct data type.

The only reason you would like to do this is to use the base class polymorphic functions. Polymorphism only works on pointers and corrections, so storing an actual object doesn’t buy you much.

+3
source

If you insist on using a base class pointer to specify a derived class, you cannot use ++ for iteration. you should do this:

 bp = d; . . . bp = &d[1]; 
+2
source

In C / C ++ / ObjectiveC, dimensions are always implicit. For one type, the compiler knows what size it is, it also knows the size of the object that the pointer points to only by the type of this pointer. Accordingly, incrementing a pointer always adds a compile-time constant to a pointer; it cannot be different, because there is no such thing as a size stored anywhere in memory. Therefore, whenever you enter a pointer from one class to another, you should no longer use pointer arithmetic, otherwise you will get undefined behavior.

As a solution, go to ShighShagh's answer and use an array of pointers.

+1
source
  • In C ++, you can use a pointer as an array to store objects of different types.
  • To create an array pointer, you simply declare a pointer variable and allocate memory using MALLOC or NEW. So you will have;
  • base * bp = (base *) malloc (sizeof (10) / sizeof (base));
  • This will create an array of size 10 pointers. Then you can use this pointer to store the base object or derived object. And you can also use a for loop to iterate through a 10-element pointer.
+1
source

Source: https://habr.com/ru/post/1499858/


All Articles