tags:

views:

348

answers:

7

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];
    ...
};
+12  A: 

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.

S.Lott
I'll often do the same thing with a typedef, knowing that it could be changed to a class definition with inheritance later. The only bad part is trying to forward-declare a typedef.
Caleb Huitt - cjhuitt
+5  A: 

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.

FrustratedWithFormsDesigner
It seems like `class Foundation : public CardStack { };` would be pretty clear documentation. The "why" should be obvious. "They do the same thing." I'm not sure it's possible to document it any more clearly. Do you have a specific issue or concern that needs to be documented above and beyond the obvious?
S.Lott
@SLott: In systems that have `class B : class A {};` I have sometimes seen situations where the same code pattern is sometimes written with objects of type A and sometimes of type B. I asked the guy who wrote some of these pieces of code why he switched back and forth: the answer was "they're the same so why should it matter?". A and B probably made sense to the original developer, but after 3 generations of maintenance coders, it had turned to an awful nightmare. There was no clear documentation as to "why". I removed B, and refactored everything to refer to A. And it all still worked fine :)
FrustratedWithFormsDesigner
...But to be fair, there have also been a FEW cases where I felt it was truly necessary. I try to avoid this pattern as much as I can, but when I HAVE to use it, I try to document as clearly as possible. Just because it's obvious to me doesn't mean it will be so obvious to the next developer, or the one after that or the one after that...
FrustratedWithFormsDesigner
+2  A: 

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.

jldupont
A: 

Nothing wrong with it, I do this often. I like it better than empty "marker" interfaces (in Java). As others have mentioned, you should probably comment on the fact that the implementation is supposed to be empty (or perhaps "reserved for future use"), but otherwise IMHO you're fine.

TMN
+2  A: 

I do it all the time for lists

public class MyObjects : List<MyObject> { }
Chad
@Chad, Wouldn't a typedef make more sense in this case?
luke
@luke, I normally code in c#, and there is no typedef in it. But it won't necessarily stay an empty class...
Chad
+1  A: 

It's good practice, since it is semantically clearer with nearly, with nearly no cost associated and allows for modifications, when the need arises for subclasses do behave differently.

Richard Durr
+1 for mentioning semantic clarity, which no other answers to date have emphasized sufficiently for my taste.
David Thornley
A: 

The way you did it the Model class it seems to me that typedef will suffice to distinguish names (and readability!): http://en.wikipedia.org/wiki/Typedef

Remon