Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to copy a poiner type to a regular static object in C++

Posted on 2012-03-29
4
Medium Priority
?
305 Views
Last Modified: 2012-03-29
I have a class..

class AClass {};

I define:

AClass*   ptrAClassInst =  new AClass();
...

AClass   anotherClassInst;

I want to copy the contents of the object pointed to by ptrAClassInst to anotherClassInst.

How?
0
Comment
Question by:prain
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
ID: 37782937
Given that the compiler-generated copy constructor is sufficiant:

anotherClassInst = *ptrAClassInst;

Open in new window


If not, you'll have to supply your own, see http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html
0
 

Author Comment

by:prain
ID: 37782997
AClass*   ptrAClassInst =  new AClass();
AClass   anotherClassInst;

anotherClassInst =  *ptrAClassInst ;    //Does not work.
0
 
LVL 86

Expert Comment

by:jkr
ID: 37783081
"Does not work." translates to what kind of error message?
0
 

Author Closing Comment

by:prain
ID: 37783549
Oops sorry. My mistake. This is good. Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

963 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question