-5

If I make a vector of pointers of type of some defined class and put a pointer to an object of type of an inherited one of the first one in it, wont the pointer pointing to next address be changed to point to shifted address because of existence of new data members in the inherited class pointed by the previous one.

so If I defined an array with new of type of the base class and then put an object of type of the driven class, how would compiler deal with the modification of the start of the address of the next pointer. shouldn't it be shifted or something like that to make some bytes available for the previous one.

asmmo
  • 6,922
  • 1
  • 11
  • 25
  • 1
    The pointer will be the same size, whether it points to a base or derived class. Of course, as you mention, the objects might have a different size if the derived class has new members. – wally May 01 '18 at 23:54
  • I'm struggling to understand your question but I think the answer is "no" - the pointer can point to a block of any size - that is the whole point (sorry;-) – John3136 May 01 '18 at 23:55
  • Does your vector contain objects or pointers? – NuPagadi May 01 '18 at 23:56
  • 3
    If you have a `vector` and force it to store a `Derived`, [you will get object slicing](https://stackoverflow.com/questions/274626/what-is-object-slicing). If you have a `vector` and store a `Derived *` you should have no problems so long as virtual functions are in play and the objects are properly polymorphic. Pointers are the same size. Mind you, you might have to keep an eye out for Cylons what with that with `Base *` hanging around. – user4581301 May 01 '18 at 23:57
  • Recommend adding a code example to better describe your intent. The reason I only wrote a comment is I'm not sure enough about what you are asking to write up a formal answer. – user4581301 May 02 '18 at 00:03
  • The type of the `std::vector` element is specified at compile time. The underlying array of elements will be spaced as per the compile time determined size. When you change the type of the elements you have to recompile and then yes, taking the addresses of the elements, you will find they are spaced further apart if the derived objects are larger. – wally May 02 '18 at 00:03
  • 2
    Just don't use pointers if you don't understand them... – Macmade May 02 '18 at 00:06
  • What would a pointer change its value just because it moved to a different place in a vector? It will still point at the same object it always pointed at. – Galik May 02 '18 at 00:26
  • Please add some example code to help us understand your question. – xskxzr May 02 '18 at 04:53
  • I wish that I can delete the question or separate it from my account – asmmo Aug 17 '20 at 21:37

1 Answers1

1

If you define a std::vector<BaseClass> where BaseClass is your base class and then put a DerivedClass type into that vector where DerivedClass is your derived class, you will get slicing. Meaning any extra data members in the derived class are lost.

Because of that, what you would normally do is define a std::vector<BaseClass *> which is a vector of base class pointers. Now you can put base class pointers and derived class pointers in your vector and there is no slicing.

You are storing pointers in the vector. All pointers are the same size.

Note, in the real world you would probably not user raw pointers but some version of a smart pointer to reduce the chance of memory leaks (i.e. forgetting to free a pointer).

Anon Mail
  • 4,660
  • 1
  • 18
  • 21