Sign in to follow this  
Mr Awesome

Combining Similar Classes

Recommended Posts

Right now I'm trying to consider how to combine two classes of mine that share a great deal of functionality. So far, I have come up with this basic idea:
/*
 * ComponentList.h
 */
template <class T>
class ComponentList
{
public:
    // various functions that do general things to
    // the components, such as insert, remove, clear,
    // transform, etc.
protected:
    list<T> Components;
};

/* 
 * VertexList.h
 */
class VertexList : public ComponentList<Vector2D>
{
public:
    // obviously more specific functions dealing
    // with a list<Vector2D>
};
Is this a good idea or even legal? Template specialization will not work here, as I need new data members and functions for each type of class. Is this the best way or am I perhaps missing something simpler. Thanks for your time.

Share this post


Link to post
Share on other sites
Quote:
// various functions that do general things to
// the components, such as insert, remove, clear,
// transform, etc.


Do a lot of those functions look like


void insert(const T& t, std::list<T>::iterator where)
{
member_list.insert(where,t);
}


this?

What's the point of wrapping the list like that? It already has all the functionality you need.

Share this post


Link to post
Share on other sites
You should consider using public inheritance when two classses have an is_a relationship. Think substitution. If you can substitute a derived class for a base class, you probably have an is_a relationship.

When you find you have two similar but unrelated classes, you might consider using private inheritance. In C++, private inheritance is usually idiomatic for an implemented-in-terms-of relationship between classes. You may even find it simplest to have a single common base class with the similar functionality, and two leaf classes using private inhertiance to provide actual consumer functionality. No one has to know what goes on in private.

Share this post


Link to post
Share on other sites
Private inheritance is not what I'm looking for. If I did straight public inheritance (no templates), I would basically be copying and pasting about 3/4 of the code and only changing a couple of things. But, if I just specialized a template, then I would lose the ability to add functions and features to specific types of that template. So, I figured that mixing the two approaches might work. However, a simple template may work for me; I'll have to analyze a little more.

And to Deyja, the insert and remove functions are not simply wrappers around the std::list functions. They take into account which components have been highlighted by the user, and other things.

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