Templates and Trading Cards

From: kelvSYC (kelvSYC_at_no.email.shaw.ca)
Date: 04/20/04


Date: Mon, 19 Apr 2004 22:20:24 GMT

I'm trying to program something along the lines of a "trading card"
idea: I have a single copy of the "card" in the program, yet there may
be multiple "instances" of the "card", with differing information (such
as the owner of the "card") in each instance.

struct Person;

Person Bob;
Person Joe;

struct Card; /* each instance of this represents different trading
cards*/
struct BaseballCard : public Card;
struct HockeyCard : public Card;

BaseballCard BabeRuthCard;
HockeyCard WayneGretzkyCard;

/* each instance of this represents a different copy of a type of
trading card */
template<class T> struct CardInstance<T> {
   Person owner;
   T& card;
};

CardInstance<BaseballCard> BobsBabeRuthCard;
CardInstance<HockeyCard> JoesWayneGretzkyCard;

How would I use templates to construct a function createCard(Person) in
Card (or its subclasses) that returns the appropriate subclass of Card
so that I can do something like this:

CardInstance<BaseballCard>& BaseballCard::createCard(Person& p);
CardInstance<HockeyCard>& HockeyCard::createCard(Person& p);

/*
The body for CardInstance<T>& T::createCard(Person& p) would be
something like

return {owner, *this};
*/

CardInstance<BaseballCard> BobsBabeRuthCard =
BabeRuthCard.createCard(Bob);
CardInstance<HockeyCard> JoesWayneGretzkyCard =
WayneGretzkyCard.createCard(Joe);

------------

Or, is this not the way to go, and that I should rethink the design?
If so, what could a possible design look like?

-- 
I am only a mirage.


Relevant Pages

  • [git patches] net driver updates
    ... struct adapter { ... /* Translate scan data returned from the card to a card independant ... +static int __init nortel_pci_init ... * The driver implements Symbol firmware download. ...
    (Linux-Kernel)
  • [PATCH 4/4] netwave: move driver to staging
    ... Move the netwave driver to drivers/staging. ... tristate "Aviator/Raytheon 2.4GHz wireless support" ... Netwave AirSurfer Wireless LAN PC Card driver ... A struct pcmcia_device structure has fields for most things that are needed ...
    (Linux-Kernel)
  • [PATCH 01/13] Char: cyclades, add tty refcounting
    ... While this is not problem for Y card handlers (they are protected ... struct cyclades_port *info; ... unsigned int char_count; ...
    (Linux-Kernel)
  • [PATCH 08/79] cyclades: add tty refcounting
    ... While this is not problem for Y card handlers (they are protected ... struct cyclades_port *info; ... unsigned int char_count; ...
    (Linux-Kernel)
  • [PATCH 012/196] nozomi driver
    ... This is a driver to control the cardbus wireless data card that works on ... Greg Kroah-Hartman did the initial driver cleanup. ... Thanks to Jiri Slaby for a lot bugfixes, cleanups ... +struct config_table { ...
    (Linux-Kernel)