Get copy of object from pointer?

M

Mike

I have a class "Call". I have instantiated a Call object in one class then
passed a pointer to that object to another class.

How do I create a *copy* of that Call object using only the pointer?
 
D

deane_gavin

Mike said:
I have a class "Call". I have instantiated a Call object in one class then
passed a pointer to that object to another class.

How do I create a *copy* of that Call object using only the pointer?

The same way you get hold of the object pointed to for any other
purpose. Dereference the pointer.

Call c1;
Call* p = &c1;

p is a pointer to a Call object.

Call c2 = *p;

c2 is created as a copy of the object pointed to by p.

Gavin Deane
 
A

Alf P. Steinbach

* Mike:
I have a class "Call". I have instantiated a Call object in one class then
passed a pointer to that object to another class.

How do I create a *copy* of that Call object using only the pointer?

If you know that the dynamic type of object pointed to is really Call,
and not some unknown class derived from Call, you can do

Call copyOfCall = *p;

or e.g.

Call* pCopyOfCall = new Call( *p );

Otherwise, if you don't want slicing you can use cloning.

FAQ item 20.8 describes the basics of cloning,
<url: <url:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8>.

A more in-depth discussion of cloning, especially the use of copy
constructors for cloning in a class hierarchy, is section 1.3.6 of
<url:
http://home.no.net/dubjai/win32cpptut/special/pointers/preview/pointers_01_beta.doc.pdf>.



Conspiracy theory of the day. How come I gave the same two URLs in this
and my previous posting to clc++? Is there a global conspiracy to ask
questions where the answers naturally list those two URLs?
 
G

Greg

The same way you get hold of the object pointed to for any other
purpose. Dereference the pointer.

Call c1;
Call* p = &c1;

p is a pointer to a Call object.

Call c2 = *p;

c2 is created as a copy of the object pointed to by p.

Not necessarily. If p were actually a pointer to a subclass of Call,
then c2 would have ended up with a "sliced" copy of p, and c2 and p
would not be of the same type. For this reason, it is best to use
whatever copy operation the class has defined. In other words, ask p to
copy itself, rather than perform a copy-by-value operation by
dereferencing the pointer and that assumes that p is a pointer to an
exact type.

Greg
 
D

davidrubin

Mike said:
I have a class "Call". I have instantiated a Call object in one class then
passed a pointer to that object to another class.

How do I create a *copy* of that Call object using only the pointer?

Typically, you don't. Either (a) 'Call' has value semantics, and you
copy the value, or you copy (b) the raw pointer, or (c) a smart pointer
(e.g., one that does reference counting).
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top