By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

fun with template-derived templates

P: n/a
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!
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Steven An" <st******@uclink.berkeley.edu> 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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.