0

I have the following problem and I hope the great community can help me. If I have a class one:

class one
{
private:
  int data;

public:
  one();
  void safe();
  void load();
};

And then 2 derivative classes of class one:

class two: public one
{
private:
  int data2;
public:
  two();
  safe();
  load();
};

class three: public one
{
private:
  int    data3;
  string data4;
public:
  three();
  void safe();
  void load();
};

And now I have to store data in these classes. The object of the classes are stored in a List. And one Element of these List can be in this example one object of class two or class three. Now If I have to save the content of a list in a file I need to get data of data2 or If I have the other object I need data3 and data4.

How can I check which derivate it is and how can I get the data of the objects? I mean that must be possible because they are both derivatives of one.

Ediac
  • 833
  • 7
  • 14
missjohanna
  • 293
  • 1
  • 2
  • 15

1 Answers1

0

If you need a list:

std::list<one*> myList;

if you need a collection like an array

std::vector<one*> myVec;

You can see more details on how to inesrt and process data from list std::list here : http://www.cplusplus.com/reference/list/list/

For std::vector<> here : http://www.cplusplus.com/reference/vector/vector/

For your second question:

How can I check which derivate it is and how can I get the data of the objects?

Since you already created an inheritance and going to keep objects as pointers in container like std::list or std::vector, system can identify the dynamic type of the objects using polymorphism and invoke the functions depends on the type of the object. More details are available here : http://www.cplusplus.com/doc/tutorial/polymorphism/

Steephen
  • 14,645
  • 7
  • 40
  • 47
  • I have to write my own list and the list is working aswell. My List is a template which can be any object of data storage. The problem I have is that I dont know how to know if there is stored data of a object from class two or from class three. So I can't just say getData() and then I get the right answer. – missjohanna Jun 21 '15 at 16:50
  • @missjohanna Are you keeping one ,two and three in a single list or do you keep separate class objects in separate list? What is the template type parameter for your list? – Steephen Jun 21 '15 at 17:05
  • I keep two and three in a single list. The template type parameter of my list are class one, so I can use class two and three for data storage. But I handle my List as a derivate of my Element class which is a template aswell. For example I initiate it with list * list1 = new list; Hope that helps to show my point – missjohanna Jun 21 '15 at 17:35
  • @missjohanna you are facing object slicing , to keep objects properly in container in your case, you should use list. – Steephen Jun 21 '15 at 17:41
  • you are right I should better use a pointer from the class one in a list. Or are you going to say I cant use my own list class? Why I should not use list * ? Isn't that possible aswell? – missjohanna Jun 21 '15 at 17:50
  • list * this you can. So now each of your objects in your container going to keep their type info also, I hope, it will solve your issue. – Steephen Jun 21 '15 at 17:54