[Techtalk] Re: [prog] C++ STL Problem

Meredith L. Patterson mlp at thesmartpolitenerd.com
Sun Oct 24 05:08:50 EST 2004

Hash: SHA1

(Standard disclaimer applies: I do this for a living, but I'm just as
error-prone as anyone else. If I've screwed something up, correct away!)

Noir wrote:
> I am working on a Aeroplane.cpp file where it asks to "make a
> PassengerList supported by GenericList template." I am utterly confused
> as to what I should do in regards to this. I have made my cpp file which
> also asks me to make a custom constructor, a copy constructor to perform
> deep copy, a destructor and a display method (I think I have done all
> these). The only problem is with the GenericList template.

- From the code you've included, I can't tell what kind of problem you're
having. Does it compile? If not, what are the errors, and if so, in what
way is the GenericList not behaving as advertised?

It would be helpful to see the definition of the GenericList template
(not necessarily all of it, if it's long). Is it based on std::list?
What's with the extra integer argument?

(I also gather that Passenger and Luggage have been defined elsewhere as
objects. Are they accessible to Aeroplane.h, perhaps via TemplateList.h?)

Also, just as an fyi ...

> Aeroplane::Aeroplane (const string Brand, const string Model,
> PassengerList *Passengers,
>                       LuggageList *Luggage) {
>     this->myPassengers = new PassengerList (*Passengers);
>     this->myLuggage = new LuggageList (*Luggage);
> }

This constructor is missing two assignments -- you're passing in a
string for Brand and a string for Model, but fBrand and fModel are never
being assigned. This is one of those silent errors, since both fBrand
and fModel will be initialised to the empty string and the compiler will
continue on its merry way. It's also something that can have you tearing
your hair out later on as you struggle to figure out why the copy
constructor isn't copying over those strings correctly -- only to
discover that they weren't being set in the first place and thus the
copy constructor is working just fine. <:)

(No, *of course* I've never done that myself ... ;)

> Aeroplane::Aeroplane (const Aeroplane& src) : fbrand(src.fBrand),
> fModel(src.fModel) {
>     this->myPassengers = new PassengerList (*src.myPassengers);
>     //might cause problem later. If so, comment out+compile and see

Nah, this is fine. Privacy isn't a problem here (I *think* because it's
a copy constructor, but I'd have to look it up to be sure; I know it
works, though) and your pointers are being used as they should be. I can
show you a known-good example if you like.

> Aeroplane::~Aeroplane()
> {
>     delete this->myPassengers;
>     delete this->myLuggage;
> }

FWIW, "delete myPassengers" and "delete myLuggage" will work fine too.
The explicit pointer dereference isn't hurting anything, though.

Meredith L. Patterson
Version: GnuPG v1.2.2 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org


More information about the Techtalk mailing list