Jump to content
  • Advertisement
Sign in to follow this  
Mr Awesome

Combining Similar Classes

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

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!