# subclass question

This topic is 4711 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Suppose I have a master class
class Master
{
//...
};

And some subclasses of it with some functions in them that are NOT functions of Master:
class Sub1 : public Master
{
//...
void a();
};

class Sub2 : public Master
{
//...
void b();
};

Now I make an std::vector of Masters:
std::vector<Master> list;

Will it now be possible to add objects of type Sub1 and Sub2 to this list, and call function a() or b() of these if they're of the correct type? For example, can I set list[4] to a Sub1 and then call list[4].a()? If yes, how? If not, is there another way to make such a list of objects of different types without using pointers? The reason I don't want to use pointers, is that I want to make some class that automaticly will make objects of different types (that are all subclass of a master class) depending on the contents it reads from a file and be able to call functions of these subclasses if I have the knowledge which class in the list is of what type. If that's all impossible, how would you make that what I said? (a class that can make objects of many different types by reading options from a file). I don't want to make different std::vectors for each object type by the way, because I want to be able to make more subclasses of the type Master without having to add a new std::vector every time, I just want an std::vector of type Master. Thanks!

##### Share on other sites
You need to store master pointers in the vector and what you want is RTTI (Run Time Type Identification) availble through dynamic_cast, note that you need at least one virtual function for that to work. But that shouldn't be a problem since you for any (base) class with virtual functions really need a virtual dtor anyhow.

So yes, it's possible but many times it's also a sign of weak design.

##### Share on other sites
loss of information is a bad thing when its needed.

A* a = new B;
B* b = (B*)a;
b->Foo();

(yes i know theres no rtti)

##### Share on other sites
As has been pointed out—yes, you can, with the caveat that you must store Master pointers, not actual instances (else your objects will get sliced, if you force them in there). However, it's generally not a good idea: If you don't want to operate uniformly on the elements in a container (even with the flexibility of polymorphic calls), you probably shouldn't store them all in the same container. I'm not saying it's never motivated, but you may want to rethink the design decision that necessitates it, if you can.

##### Share on other sites
Might it be a good idea to totally change my design then?

Namely, making sure all the public functions of all the subclasses are overloaded versions of functions that exist in the Master class, and making for example a function "setParameter(Option option)" that sets a parameter of the subclass, and each subclass will interpret the Option in it's own way. Currently, each subclass has totally different functions with different names because they also have totally different parameters, but it would be possible to use a general setParameter function instead.

##### Share on other sites
It depends. Do they conceptually perform analogous operations on the same data, varying in details with the subclass? If so, use overriding and polymorphic dispatch. Do they conceptually perform different operations? If so, don't abuse an existing system into doing something counterintuitive, but store the items in different containers.

##### Share on other sites
Maybe it would be better if the class read the data it needed from somewhere else, as it is most likely to know what it needs.