Ananth wrote:
> Thomas Hruska <[EMAIL PROTECTED]> wrote:
>>  To do an array of base, you have to be a bit more creative:  SmartPtr<>
>>  with a Clone()/Copy() and have a Clone()/Copy() with a variant return
>>  type in both the base and derived classes and then use a Block<
>>  SmartPtr<Base> > as the wrapper.  That should work on every modern
>>  compiler (assuming it supports variant return types).
> 
> Okay, its midnight here and your approach is way over my head now!
> Will look at it my morning and try to understand it.
> Thanks for your thoughts Thomas, I highly value your comments in this forum.
> I think I still have a couple of discussions I had with you from way
> back in 2001, have saved it somewhere.
> 
> Cheers
> - Ananth

I'll clarify what I mean just a bit:

Apply a covariant return type with a Copy() (aka Clone()) function to 
both the Base and Derived classes:

class Base
{
...
   virtual Base *Copy() const
   {
     return new Base(*this);
   }
...
};

Repeat for Derived:

class Derived
{
...
   virtual Derived *Copy() const
   {
     return new Derived(*this);
   }
...
};

Then, use SmartPtr<>:

Derived TempDerived;
SmartPtr<Base> TempObj;
Block< SmartPtr<Base> > TempBlock;

...Do stuff with TempDerived...
TempObj.Copy(TempDerived);
TempBlock += TempObj;

...Do some different stuff with TempDerived...
TempObj.Copy(TempDerived);
TempBlock += TempObj;


Program will clean up after itself just fine.  Block<> and SmartPtr<> 
are from Safe C++.  It works because SmartPtr<>.Copy() uses the Copy() 
found in Derived to copy the object (thanks to some 'virtual' keyword 
magic).  So you could also use a plain-ol' Base as well (it would use 
the Copy() found in Base instead).

Obviously this approach uses more RAM.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to