Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is creating an empty class purely to distinguish it from another class good practice?

Tags:

c++

I have a class CardStack. I have several classes that inherit from CardStack e.g. Cascade, Deck, Foundation etc.

Foundation doesn't need to add any functionality to CardStack, but for display purposes my app needs to know which of the CardStacks are actually Foundations.

Incidentally, I have no such function CardStack.Display() (I'm using a model-view-controller pattern where the View object simply queries the Model to find out what type of objects it's dealing with).

It seems OK to me, but is there any reason not to do this?

class Foundation : public CardStack
{

};

class Model
{
    Cascade cascade[10];
    Foundation foundations[10];
    ...
};
like image 923
BeeBand Avatar asked Jan 20 '10 17:01

BeeBand


3 Answers

Nothing wrong with this.

Do it all the time.

In the future, there may be a difference in structure, behavior or implementation. For now, they happen to share a lot of common features.

like image 146
S.Lott Avatar answered Oct 19 '22 23:10

S.Lott


I don't see any technical problem with it, so maybe you're doing this for semantic reasons. In that case, make sure you document the reason it VERY CLEARLY so maintenance programmers later on don't try and change things.

like image 20
FrustratedWithFormsDesigner Avatar answered Oct 20 '22 00:10

FrustratedWithFormsDesigner


Yep, this is valid and useful. An empty class can act as placeholder for future functionality (as example). Of course, a bit of documentation is in order if the class in question is "connected" to the program in any way ;-)

In your case above, the C++ code generated won't be burdened... but readability of your code is increased.

like image 39
jldupont Avatar answered Oct 20 '22 01:10

jldupont