Re: reference to pointer of derived class

From: Bogdan Sintoma (bogy01_at_yahoo.com)
Date: 01/27/05


Date: 27 Jan 2005 03:32:18 -0800

Hi

Moes wrote:
> Thanks for the response. The code in question is actually in the
operator=
> of a class in which I have a pointer to a pure virtual base class
(such as
> "Car" in my example). I have to set that pointer to a new derived
class
> (either new TOyota() or new Honda() in the example) depending on what
type
> the source class's member points to. how would this be achieved
without a
> switch on type?
As Jerry already told you: virtual functions.

>(In the real class it's a vector of pointers, instad of a
> single member pointer, in case that matters.)
It doesn't matter.

> struct Garage{
> Garage(){ pCar = NULL; }
Garage() : pCar(0) {}

> ~Garage(){ if( pCar ) delete pCar; } // Will this call the correct
> destructor for derived classes?
Yes, if the Car class has a virtual destructor.
And you don't need "if". If the pCar is NULL delete will do nothing. If
it is not NULL but already deleted you are in trouble anyway :).

> Garage& operator=( const Garage &source ){
> if( pCar )
> delete pCar; // may need the below switch here too so the
correct
> destructor is called?
In your "real" code did you test for self-assignment?
You should implement the op. = in terms of the copy constructor and
swap.
Or, try this:
|Garage& operator=( const Garage &source )
|{
| if( this != &source)
| {
| delete pCar;
| pCar = 0; //NULL
| if( source.pCar )
| pCar = source.pCar->clone();
| }
| return *this;
|}
Where clone() is a virtual function declared in the base class and
implemented in each class derived from Car something like:
|virtual Car* clone() const
|{
| return new ThisCarSpecificClass( *this );
|}
Here "virtual" is optional, but I prefer to write it anyway.

And by the way, please see the faq:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.6
.
.
Best regards,
Bogdan Sintoma



Relevant Pages

  • Re: reference to pointer of derived class
    ... > I started to switch this from using a reference to a pointer, ... of a class in which I have a pointer to a pure virtual base class (such as ... Garage(){pCar = NULL;} ... destructor for derived classes? ...
    (comp.lang.cpp)
  • Re: is such exception handling approach good?
    ... No exception from this rule that I know of (no pun ... For the object pointed by smart pointer, ... Free that resource in the destructor. ... For resources other than the heap memory, you would use other classes to ...
    (microsoft.public.vc.language)
  • Re: Returning an unknown number of types/values
    ... >> destructor is empty the Base destructor will still clear up the memory ... This array was allocated to the unsigned char pointerin the Base class's ... The useful thing about this pointer ... THe probelm appears when you try to overload the second subscript operator ...
    (alt.comp.lang.learn.c-cpp)
  • Re: STL vector<float> crash
    ... Which is when the destructor is called. ... Iterators aren't unsafe, but they aren't guaranteed to be pointer ... I can see where using the feof() return to ...
    (comp.lang.cpp)
  • Re: problems with "new"
    ... I havent deleted somefoo. ... Set a breakpoint on somefoo's destructor ... make sure that there's no possibility that the pointer has been passed ... gSomefoo = fooFactory.GetInstance; ...
    (microsoft.public.vc.language)

Loading