fun with template-derived templates

Discussion in 'C++' started by Steven An, Aug 28, 2004.

  1. Steven An

    Steven An Guest

    OK, i'll try to be as clear as possible with my question..hehe.

    I'm basically trying to resolve the "parking lot of cars" and "parking
    lot of vehicles" problem using templates. My rationale is this: make
    the template ParkingLot<TVehicle> that derives from list<TVehicle>,
    and which has functions like "findByName" (all Vehicle's have names)
    that all ParkingLots will need. So this looks like:

    template<class TVehicle>
    class ParkingLot : public list<TVehicle>
    {
    public: TVehicle & findByName( const string & name )
    {
    iterator v;
    for( v = begin(); v != end(); v++ ) {
    if( v->getName() == name )
    return *v;
    }
    }
    }

    And then, I would use this class like this (where Car is derived from
    Vehicle)

    ParkingLot<Car> VIP_Lot;
    Car CEO_Car = VIPLot.findByName( "CEO" );

    And this is all fine and dandy, cuz I cannot cast VIP_Lot as a generic
    Vehicle Lot, so it's impossible to accidentally park a Tank into my
    VIP_Lot.

    So two issues:
    1) How can I make sure that no one can make a ParkingLot<Apple>? Ie.
    how can I make sure that the TVehicle template param is a type that's
    derived from Vehicle? I assume the compiler would check to make sure
    that the getName() method exists in the type, but Apple::getName
    probably would exist! So that would be kinda bad (or would it? that
    functionality wouldn't really hurt now that I think about it..).
    2) As you can see, I defined findByName in the declaration. When I
    tried to define it outside the header file, VS.NET gives me linker
    errors. I think this has something to do with "export"...can anyone
    clarify this?

    Thanks ahead of time!
     
    Steven An, Aug 28, 2004
    #1
    1. Advertising

  2. "Steven An" <> wrote...
    > OK, i'll try to be as clear as possible with my question..hehe.
    >
    > I'm basically trying to resolve the "parking lot of cars" and "parking
    > lot of vehicles" problem using templates. My rationale is this: make
    > the template ParkingLot<TVehicle> that derives from list<TVehicle>,
    > and which has functions like "findByName" (all Vehicle's have names)
    > that all ParkingLots will need. So this looks like:
    >
    > template<class TVehicle>
    > class ParkingLot : public list<TVehicle>
    > {
    > public: TVehicle & findByName( const string & name )
    > {
    > iterator v;
    > for( v = begin(); v != end(); v++ ) {
    > if( v->getName() == name )
    > return *v;
    > }
    > }
    > }
    >
    > And then, I would use this class like this (where Car is derived from
    > Vehicle)
    >
    > ParkingLot<Car> VIP_Lot;
    > Car CEO_Car = VIPLot.findByName( "CEO" );
    >
    > And this is all fine and dandy, cuz I cannot cast VIP_Lot as a generic
    > Vehicle Lot, so it's impossible to accidentally park a Tank into my
    > VIP_Lot.
    >
    > So two issues:
    > 1) How can I make sure that no one can make a ParkingLot<Apple>? Ie.
    > how can I make sure that the TVehicle template param is a type that's
    > derived from Vehicle? I assume the compiler would check to make sure
    > that the getName() method exists in the type, but Apple::getName
    > probably would exist! So that would be kinda bad (or would it? that
    > functionality wouldn't really hurt now that I think about it..).


    If you thought about it, you could probably see that your ParkingLot isn't
    really a parking lot, but rather a CollectionThatAllowsSearchingByName.
    As you put more functionality that actually makes it a parking lot, the
    danger of somebody using it to park apples will deminish.

    > 2) As you can see, I defined findByName in the declaration. When I
    > tried to define it outside the header file, VS.NET gives me linker
    > errors. I think this has something to do with "export"...can anyone
    > clarify this?


    I think this is covered in the FAQ.

    Victor
     
    Victor Bazarov, Aug 28, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    65
    Views:
    1,803
    Mabden
    May 18, 2004
  2. Replies:
    1
    Views:
    407
    myork
    May 23, 2007
  3. Replies:
    1
    Views:
    398
    Victor Bazarov
    May 23, 2007
  4. dolphin
    Replies:
    4
    Views:
    331
    Jorgen Grahn
    Aug 25, 2007
  5. er
    Replies:
    2
    Views:
    525
Loading...

Share This Page