My goal is to store elements of different data types in one vector. A ball as well as an cuboid is an object.
//header.h
struct Object {
};
struct Ball : Object {
int diameter;
int surface;
};
struct Cuboid : Object {
int length;
int width;
int height;
int surface;
};
std::vector<Object *> myObjects;
//source.cpp
myObjects.push_back(new Ball);
Now I would like to access the members of ball by:
myObjects[0]->diameter = 8; //not possible
The IDE only shows me the option accessing constructor of objects:
myObjects[0]->Object
I found some useful articles:
A true heterogenous container in c++
SO: Vector that can have 3 different data types C++
Do I have to go this way? I really just would like to store ball and cuboid in one vector and later on access members of ball and cuboid.