Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > inheritance and polymorfism

Reply
Thread Tools

inheritance and polymorfism

 
 
Tony Johansson
Guest
Posts: n/a
 
      08-19-2005
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


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Stefan_N=E4we?=
Guest
Posts: n/a
 
      08-19-2005
Tony Johansson wrote:
> 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.


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

/S
 
Reply With Quote
 
 
 
 
msalters
Guest
Posts: n/a
 
      08-19-2005

Tony Johansson schreef:

> Hello experts!
>
> How is it possible to copy concrete object in a correct way without knowing
> each object specific type.


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

Regards,
Michiel Salters

 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      08-19-2005

Stefan Näwe wrote:
> Tony Johansson wrote:
> > 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.

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


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
};

 
Reply With Quote
 
Marc Mutz
Guest
Posts: n/a
 
      08-19-2005
Earl Purple wrote:
> try
> {
> itsAPtr*=*rhs.itsAPtr->clone();
> //*anything*else
> }
> catch*(*...*)
> {
> delete*itsAPtr;
> throw;
> }
>


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

Marc

 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
mul. inheritance & overloading operator new/delete solved by virtual base inheritance? cppsks C++ 0 10-27-2004 07:49 PM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57