F
F
Flaker2013-11-27 17:49:01
OOP
Flaker, 2013-11-27 17:49:01

How to store objects (or pointers to them) in an array (std::vector) in C++?

Is it possible to store objects ( or pointers to them ) of different classes in an array? And How?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vit, 2013-11-27
@Flaker

For example, by inheriting your classes from one base class, you make a vector for this base class, respectively. Then you can put an object of any class inherited from the base one into it.
In c# and java in general, all classes are inherited from the base class Object, so you can put an object of any class in a container for Object. Whether there is such a standard base class in the pros, unfortunately I can’t say, I’m not strong in c ++. But you can make your own.

T
Teivaz, 2013-11-28
@Teivaz

Well, or in order not to get into family ties, you can use, for example:
std::vector<boost::any> containsAny

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question