Connecting Tech Pros Worldwide Help | Site Map

inheritance and polymorfism

  #1  
Old August 19th, 2005, 01:15 PM
Tony Johansson
Guest
 
Posts: n/a
Hello experts!

How is it possible to copy concrete object in a correct way without knowing
each object specific
type. Can you give some code example how this is done.

//Tony


  #2  
Old August 19th, 2005, 01:35 PM
msalters
Guest
 
Posts: n/a

re: inheritance and polymorfism



Tony Johansson schreef:
[color=blue]
> Hello experts!
>
> How is it possible to copy concrete object in a correct way without knowing
> each object specific type.[/color]

What's your problem? There are at least two solutions, with templates
or
with virtual functions. Or is this homework?

Regards,
Michiel Salters

  #3  
Old August 19th, 2005, 01:35 PM
Stefan Näwe
Guest
 
Posts: n/a

re: inheritance and polymorfism


Tony Johansson wrote:[color=blue]
> Hello experts!
>
> How is it possible to copy concrete object in a correct way without knowing
> each object specific
> type. Can you give some code example how this is done.[/color]

Can you give some code example how this can not be done or what your
problem is ?

/S
  #4  
Old August 19th, 2005, 02:05 PM
Earl Purple
Guest
 
Posts: n/a

re: inheritance and polymorfism



Stefan Näwe wrote:[color=blue]
> Tony Johansson wrote:[color=green]
> > Hello experts!
> >
> > How is it possible to copy concrete object in a correct way without knowing
> > each object specific
> > type. Can you give some code example how this is done.[/color]
>
> Can you give some code example how this can not be done or what your
> problem is ?
>[/color]

Given that the topic is called "inheritance and polymorphism" I assume
he is looking for a virtual clone function.

class cloneable
{
public:
virtual cloneable* clone() const = 0;
};

class ABase : public cloneable
{
// whatever stuff here
};

class ADerived : public ABase
{
public:
ADerived * clone() const { return new ADerived( *this ); }
};

class AContainer
{
private:
ABase * itsAPtr;
public:
AContainer( const AContainer& rhs )
: itsAPtr( 0 )
{
try
{
itsAPtr = rhs.itsAPtr->clone();
// anything else
}
catch ( ... )
{
delete itsAPtr;
throw;
}
}
// other stuff
};

  #5  
Old August 19th, 2005, 09:15 PM
Marc Mutz
Guest
 
Posts: n/a

re: inheritance and polymorfism


Earl Purple wrote:[color=blue]
> try
> {
> itsAPtr*=*rhs.itsAPtr->clone();
> //*anything*else
> }
> catch*(*...*)
> {
> delete*itsAPtr;
> throw;
> }
>[/color]

std::auto_ptr<ABase> tmp( rhs.itsAPtr->clone() );
// anything else
itsAPtr = tmp.release();

Marc

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it possible to use inheritance here or some other construction to make the code less tony answers 1 April 7th, 2006 12:35 PM
Multiple Inheritance nicolas.hilaire@motorola.com answers 9 December 5th, 2005 09:25 AM
public inheritance wth advantage and disdvantage Tony Johansson answers 2 August 16th, 2005 05:45 PM
Where to put attribute when using Inheritance Tony Johansson answers 3 July 23rd, 2005 04:46 AM