Making a copy of an object from pointers

  • Thread starter Kasper Middelboe Petersen
  • Start date
K

Kasper Middelboe Petersen

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
 
I

Ian Collins

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();
 
J

Juha Nieminen

Ian Collins said:
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); }
};
 
I

Ian Collins

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!
 
F

Fabrizio J Bonsignore

  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
 
F

Fabrizio J Bonsignore

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top