Templates and Trading Cards
From: kelvSYC (kelvSYC_at_no.email.shaw.ca)
Date: 04/20/04
- Next message: Eric: "[Q] Template: Undefined Constructors"
- Previous message: Claudio Jolowicz: "Re: Ctor initialization lists: three details beyond the FAQ-lite"
- Next in thread: Dave Moore: "Re: Templates and Trading Cards"
- Reply: Dave Moore: "Re: Templates and Trading Cards"
- Reply: Siemel Naran: "Re: Templates and Trading Cards"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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.
- Next message: Eric: "[Q] Template: Undefined Constructors"
- Previous message: Claudio Jolowicz: "Re: Ctor initialization lists: three details beyond the FAQ-lite"
- Next in thread: Dave Moore: "Re: Templates and Trading Cards"
- Reply: Dave Moore: "Re: Templates and Trading Cards"
- Reply: Siemel Naran: "Re: Templates and Trading Cards"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|