Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > fun with template-derived templates

Reply
Thread Tools

fun with template-derived templates

 
 
Steven An
Guest
Posts: n/a
 
      08-28-2004
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!
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-28-2004
"Steven An" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
3 PIX VPN questions - FUN FUN FUN frishack@gmail.com Cisco 3 03-16-2006 02:25 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
OT: Wednesday follow-up-to-Tuesday-Fun Fun Ken Briscoe MCSE 0 07-14-2004 01:41 PM
Fun fun fun Luke Computer Support 3 10-07-2003 03:45 PM



Advertisments