Making a copy of an object from pointers

Discussion in 'C++' started by Kasper Middelboe Petersen, Sep 18, 2010.

  1. Hello,

    To illustrate my problem I'll use the common classes:

    Class Figure {
    ..
    }

    Class Circle : public Figure {
    ..
    }

    Figure *f = new Circle();
    Figure *fcopy;

    Now my problem is, how do I get the fcopy pointer to point to a copy
    of the object f points to? I do not know more about the object than
    than its a Figure (it could be a triangle or whatever too).


    Thanks,
    Kasper
    Kasper Middelboe Petersen, Sep 18, 2010
    #1
    1. Advertising

  2. Kasper Middelboe Petersen

    Ian Collins Guest

    On 09/18/10 02:09 PM, Kasper Middelboe Petersen wrote:
    > Hello,
    >
    > To illustrate my problem I'll use the common classes:
    >
    > Class Figure {
    > ..
    > }
    >
    > Class Circle : public Figure {
    > ..
    > }
    >
    > Figure *f = new Circle();
    > Figure *fcopy;
    >
    > Now my problem is, how do I get the fcopy pointer to point to a copy
    > of the object f points to? I do not know more about the object than
    > than its a Figure (it could be a triangle or whatever too).


    Look up the factory pattern.

    You can add a virtual "clone" method to Figure with each derived class
    implementing it to return an instance of its own type. For example:

    Class Figure {
    virtual Figure* clone() = 0;
    }

    Class Circle : public Figure {
    Circle( const Circle* );
    Circle* clone( const Figure* p ) { return new Circle(this); }
    }

    Figure *fcopy = f->clone();

    --
    Ian Collins
    Ian Collins, Sep 18, 2010
    #2
    1. Advertising

  3. Ian Collins <> wrote:
    > Class Circle : public Figure {
    > Circle( const Circle* );
    > Circle* clone( const Figure* p ) { return new Circle(this); }
    > }


    Wait, where is this "kind of a copy constructor, but takes a pointer
    instead of a reference" pattern coming from? And what's the 'p' parameter
    for? It's not even being used in the clone() function. And isn't the
    'class' keyword written with a small 'c'? And aren't those member
    functions declared private there?

    Wouldn't the usual way be:

    class Circle : public Figure
    {
    public:
    Circle( const Circle& );
    Circle* clone() const { return new Circle(*this); }
    };
    Juha Nieminen, Sep 19, 2010
    #3
  4. Kasper Middelboe Petersen

    Ian Collins Guest

    On 09/19/10 05:33 PM, Juha Nieminen wrote:
    > Ian Collins<> wrote:
    >> Class Circle : public Figure {
    >> Circle( const Circle* );
    >> Circle* clone( const Figure* p ) { return new Circle(this); }
    >> }

    >
    > Wait, where is this "kind of a copy constructor, but takes a pointer
    > instead of a reference" pattern coming from? And what's the 'p' parameter
    > for? It's not even being used in the clone() function. And isn't the
    > 'class' keyword written with a small 'c'? And aren't those member
    > functions declared private there?
    >
    > Wouldn't the usual way be:
    >
    > class Circle : public Figure
    > {
    > public:
    > Circle( const Circle& );
    > Circle* clone() const { return new Circle(*this); }
    > };


    Yes it was bollocks, I don't know why I posted that.

    I'll use the standard excuse round here - 600 aftershocks!

    --
    Ian Collins
    Ian Collins, Sep 19, 2010
    #4

  5. > Yes it was bollocks, I don't know why I posted that.


    It got the idea across tough and solved my problem - thanks :)


    /Kasper
    Kasper Middelboe Petersen, Sep 19, 2010
    #5
  6. On Sep 19, 1:33 am, Juha Nieminen <> wrote:
    > Ian Collins <> wrote:
    >   Wait, where is this "kind of a copy constructor, but takes a pointer
    > instead of a reference" pattern coming from?


    The thread is Assigment operator=/copy constructor/temporaries,
    BROKEN!

    AO::AO(AO *a);

    This constructor takes a pointer to the same class instead of a
    reference, you get the pointer from a signature like:

    AO Function();

    like this:

    AO a(&Function());

    AO Function() returns a temporary by value. The issue is whether the
    temporary can be assumed valid once entering the pointer copy
    constructor and under what conditions. It is no issue to use a pointer
    to construct an object of the same type, it saves one * if you use
    pointers, though it is useful to determine if the initializiting
    object exists or not (first instance of the class).

    Danilo J Bonsignore
    Fabrizio J Bonsignore, Sep 19, 2010
    #6
  7. On Sep 17, 10:21 pm, Ian Collins <> wrote:
    > On 09/18/10 02:09 PM, Kasper Middelboe Petersen wrote:
    >
    > Look up the factory pattern.
    >
    > You can add a virtual "clone" method to Figure with each derived class
    > implementing it to return an instance of its own type.  For example:
    >
    > Class Figure {
    >    virtual Figure* clone() = 0;
    > }


    Figure* clone(); is not a factory, it is a clone function. The factory
    pattern implies a secondadry class object that builds objects for a
    hierarchy and may have a complex relationship to that hierarchy or be
    just a switch class interpreting some context data to decide the class
    to build.

    Danilo J Bonsignore
    Fabrizio J Bonsignore, Sep 19, 2010
    #7
  8. Kasper Middelboe Petersen

    Ian Collins Guest

    On 09/20/10 10:34 AM, Fabrizio J Bonsignore wrote:
    > On Sep 17, 10:21 pm, Ian Collins<> wrote:
    >> On 09/18/10 02:09 PM, Kasper Middelboe Petersen wrote:
    >>
    >> Look up the factory pattern.
    >>
    >> You can add a virtual "clone" method to Figure with each derived class
    >> implementing it to return an instance of its own type. For example:
    >>
    >> Class Figure {
    >> virtual Figure* clone() = 0;
    >> }

    >
    > Figure* clone(); is not a factory, it is a clone function.


    I didn't say otherwise. I made two separate suggestions.

    --
    Ian Collins
    Ian Collins, Sep 19, 2010
    #8
    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. VisionSet
    Replies:
    8
    Views:
    4,877
    Tris Orendorff
    Apr 29, 2004
  2. Alex
    Replies:
    2
    Views:
    1,202
  3. Replies:
    26
    Views:
    2,090
    Roland Pibinger
    Sep 1, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    656
  5. Max Williams
    Replies:
    5
    Views:
    120
    Brian Candler
    May 28, 2009
Loading...

Share This Page