Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 19th, 2005, 01:15 PM
Tony Johansson
Guest
 
Posts: n/a
Default inheritance and polymorfism

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
Default 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
Default 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
Default 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
Default 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles