Yuri Bilas Yuri Bilas - 2 months ago 3
C++ Question

Vector of parents and childs C++

I want to make a vector of parent and child elements. I tested my code with only one child and that works nice. But when i added one more child (

), i see a problem.

When i try to get value of
expected output is : 5 7 11, but i get value of
variable (2);


#include <SFML/Graphics.hpp>
#include "Helper.h"
#include <vector>
#include <iostream>
using namespace sf;

int main() {
std::vector<Parent*> arr1;
arr1.push_back(new Child(3, 4, 5, 6));
arr1.push_back(new Parent(7, 8));
arr1.push_back(new Child2(9, 10, 11, 12));

std::cout << static_cast<Universal*>(arr1.at(0))->dx << std::endl;
std::cout << static_cast<Universal*>(arr1.at(1))->x << std::endl;
std::cout << static_cast<Universal*>(arr1.at(2))->px << std::endl;
return 0;


#include "Helper.h"

Parent::Parent(int x, int y) {
this->x = x;
this->y = y;

Child::Child(int x, int y, int dx, int dy) : Parent(x, y) {
this->dx = dx;
this->dy = dy;

Child2::Child2(int x, int y, int px, int py) : Parent(x, y) {
this->px = px;
this->py = py;

Universal::Universal(int x, int y) : Parent(x, y) {}


class Parent {
int x, y;
int type = 0;

Parent(int x, int y);

class Child : public Parent {
int dx, dy;
int type = 1;

Child(int x, int y, int dx, int dy);

class Child2 : public Parent {
int px, py;
int type = 2;

Child2(int x, int y, int px, int py);

class Universal : public Parent {
int dx, dy, px, py;

Universal(int x, int y);


Universal is not a parent or child of Child2. As such, the static_cast will not work.

Child2 has the following class members:

int    int    int
 px     py    type

Universal, on the other hand, contains the following class members:

int    int    int    int
 dx      dy    px     py

And that's why when the code reads px, it ends up getting the value of type. Your compiler simply places each class member, consecutively as part of its class, irrespective of the name of the class member. type in one of the classes ends up occupting the same relative position, the offset, from the beginning of the class, in memory, as px in the other one. The classes contain int class members. type is the third int in one of the classes, and type is the third int in the other one.

Just because two classes have a class member of the same name it doesn't mean that the class member will have the same internal position in memory, as the member of its class.

In fact, the C++ standard doesn't really give you many guarantees. That there's no guarantee that class members will be arranged in any particular order, in memory, anyway. There are actually a few rules there, but they're irrelevant for the purpose of this question.

Even if both classes have their fields listed, in a class declaration, in some particular order, one should not end up relying on the fact that a very rude static_cast will end up with a different class with the fields with the same name having the same values as the other class.

The correct solution for what you're trying to do is to use virtual class methods.