inheritance and polymorfism

Discussion in 'C++' started by Tony Johansson, Aug 19, 2005.

  1. 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
     
    Tony Johansson, Aug 19, 2005
    #1
    1. Advertising

  2. 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
     
    =?ISO-8859-1?Q?Stefan_N=E4we?=, Aug 19, 2005
    #2
    1. Advertising

  3. Tony Johansson

    msalters Guest

    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
     
    msalters, Aug 19, 2005
    #3
  4. Tony Johansson

    Earl Purple Guest

    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
    };
     
    Earl Purple, Aug 19, 2005
    #4
  5. Tony Johansson

    Marc Mutz Guest

    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
     
    Marc Mutz, Aug 19, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,184
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    843
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,061
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,944
    Mike Schilling
    Feb 27, 2008
  5. Rouslan Korneychuk
    Replies:
    8
    Views:
    618
    Rouslan Korneychuk
    Feb 10, 2011
Loading...

Share This Page