fun with template-derived templates

S

Steven An

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!
 
V

Victor Bazarov

Steven An said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top