Sign in to follow this  
Lode

subclass question

Recommended Posts

Lode    1003
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 this post


Link to post
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 this post


Link to post
Share on other sites
Miserable    606
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 this post


Link to post
Share on other sites
Lode    1003
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 this post


Link to post
Share on other sites
Miserable    606
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 this post


Link to post
Share on other sites
petewood    819
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this