Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Making a copy of an object from pointers

Reply
Thread Tools

Making a copy of an object from pointers

 
 
Kasper Middelboe Petersen
Guest
Posts: n/a
 
      09-18-2010
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
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-18-2010
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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      09-19-2010
Ian Collins <(E-Mail Removed)> 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); }
};
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-19-2010
On 09/19/10 05:33 PM, Juha Nieminen wrote:
> Ian Collins<(E-Mail Removed)> 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 *******s, I don't know why I posted that.

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

--
Ian Collins
 
Reply With Quote
 
Kasper Middelboe Petersen
Guest
Posts: n/a
 
      09-19-2010

> Yes it was *******s, I don't know why I posted that.


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


/Kasper
 
Reply With Quote
 
Fabrizio J Bonsignore
Guest
Posts: n/a
 
      09-19-2010
On Sep 19, 1:33*am, Juha Nieminen <(E-Mail Removed)> wrote:
> Ian Collins <(E-Mail Removed)> 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
 
Reply With Quote
 
Fabrizio J Bonsignore
Guest
Posts: n/a
 
      09-19-2010
On Sep 17, 10:21*pm, Ian Collins <(E-Mail Removed)> 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
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-19-2010
On 09/20/10 10:34 AM, Fabrizio J Bonsignore wrote:
> On Sep 17, 10:21 pm, Ian Collins<(E-Mail Removed)> 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
 
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
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
making a name like "Copy of...", "Copy 2 of..." Max Williams Ruby 5 05-28-2009 04:05 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM



Advertisments