473,378 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Get copy of object from pointer?

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?
Nov 27 '05 #1
4 44307
Mike wrote:
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

Nov 27 '05 #2
* 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?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 27 '05 #3
de*********@hotmail.com wrote:
Mike wrote:
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.


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

Nov 27 '05 #4

Mike wrote:
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).

Nov 27 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: pcouas | last post by:
Hi, What is best way to Copy object that extend TagSupport ? Regards Philippe
30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
8
by: Joe Cipale | last post by:
I have a class defined as follows: public: char Date; char weight; char Workout; char Event; TDaily* nxt_Daily; I have defined my constructor/copy methods as shown:
4
by: xuatla | last post by:
Hi, How to copy a pointer to another pointer? Can I do in the following way: // START double *copyfrom = new double; double *copyto = new double;
25
by: MC | last post by:
Hi I have the following piece of code. int *p; p = malloc(10); int *j; j = p; free(p); Is this legal ? will j still point to the same
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
1
by: tiffini | last post by:
Hi, I have an interesting question about an object pointer. If you have Class A <---- Class B <--- Class C <- Class D / /
10
by: Jason Who | last post by:
I need to make a quick raw copy of a large block of data from one pointer to another. Marshal.Copy let me copy from pointer to array and another call can take it from aray to pointer. That is...
0
by: Angus | last post by:
I have a function called subscribe which passes a pointer to a 'subscribing' object to a class which will call functions on this object. Here is the function: void...
3
by: Immortal_Nephi | last post by:
Sometimes, unsigned char array is located in the file scope. You define A Object and B Object. A Object and B Object need to share unsigned char array. They are very different object. They are...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.