Class instance restriction

Hi, I'm writing a class Card (playing cards), when I come across the member variables, I'm wondering what type of variables I should use for the suits and the ranks of the cards (well, no jokers atm):

- integers
* well...just as representations...but that may confuse the users in the future because suits and ranks are really not integers...

- enumerations
like enum SUIT = { SPADE = 1, HEART, /*...*/ };
* later when I come across decks of cards I'd like to sort the cards by casting them into, say, int, for the sake of easy comparisons (using > or < or ...), but I don't want to destroy the strong typing...
* plus, the purpose of enum is to make constants but a suit does not have to be constant (not in terms of a single card, but like decks of cards)
* I don't want to make these things global

- classes
make a SUIT class
Here comes my concerns:
* what should the member variables be, an int representing a SUIT? Are we going back to the "integers" method?
* There are only four suits. Firstly, I want to instantiate four const objects for (all) future uses (such as in the class Deck), how and where(in header or cpp) should I declare them?; secondly, how do you prevent the users from creating their own, new SUITs?

Thank you for reading :) I would really appreciate if you come up if with better alternatives.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
class Card{
    //...
    enum{
        SPADE=0,
        CLUB,
        HEART,
        DIAMOND
    };
    int suit;
    //...
};

Keep it simple.
Last edited on
Do you make use of the int to create an object of Card? In other words, does the constructor take the form Card(int)?
Yes.
If you want stronger typing, you can give the enum a name (e.g. SUIT) and have the constructor take that, instead. Then, you construct by passing Card::SPADE, etc. Conversion int->enum has to be explicit, but enum->int may be implicit.
But really, there isn't much difference. The compiler always treats enums as integers, anyway.
Last edited on
I see. Thank you very much.
Topic archived. No new replies allowed.