Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

object copy

Posted on 2011-03-14
6
Medium Priority
?
301 Views
Last Modified: 2012-05-11
is this allowed in C++

in my .h file

mClass *mObj.

inside .cpp function

void assign(myClass* inObj) {
  mObj = inObj; // Will this do the copy properly?  
}

do I have to clean/destroy mObj in the destructor?
0
Comment
Question by:learningunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 35128940
That will copy the pointer not the object. Maybe you mean...

*mObj = *inObj;

??

>> do I have to clean/destroy mObj in the destructor?
What do you mean?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35128945
That question cannot be answered with so little information.

There are several aspects that could make this fail, but under the right circumstances, what you propose is valid.

Some questions for you :

(a) in the header file : is that a declaration or a definition ?

(b) is mObj it in the class scope or in the global scope ? Static or not ?

(c) do you transfer ownership of the inObj object to the class or not ?

(d) does mObj already point to an object before assign is called or not ?
0
 
LVL 35

Accepted Solution

by:
sarabande earned 1000 total points
ID: 35130156
as you speak of a destructor i assume the mObj is a member variable of a class.

if so, the assign function of the cpp must have class scope.

   void whateverclass::assign(myClass* inObj) {

any constructor of the class which has mObj as a member must have an initialisation like

  whateverclass::whateverclass() : mObj(NULL) {}

the assign function then should make a copy of the object as shown by evilrix what would require a valid copy constructor of myClass.

   if (mObj == NULL)
     mObj = new myClass;
  *mObj = *inObj;

if all done so far the destructor of whateverclass can destroy mObj.

    delete mObj;

if you only copied the pointer in the assign function, likely another (class) is responsible for the pointer and you can't delete it. that would be bad design though.


Sara
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 35166013
learningunix:

for your snippet I'll make an assumption

---assign() and mObj are members of the same class.

Now assign does not do a copy it just copies the pointer as has been pointed by other experts.

>>"do I have to clean/destroy mObj in the destructor?"
Once again this question will give rise to ambiguous answers. You don't have to destroy mObj in the destructor. The object pointed to by inObj and mObj are the same. Bear this in mind.
So if you delete mObj then, after the place you called assign you may no longer be able to use the object pointed to by inObj

If you no longer need to use the object pointed to by inObj after the place you called assign() then you can delete it. But this can be done in any other member function as well not necessarily in the destructor.
This is called "transfer of ownership"



"
 if (mObj == NULL)
     mObj = new myClass;
  *mObj = *inObj;"

sara this is not entirely correct. You are creating a new object but then just overlaying it with inObj's contents. This really wouldn't be a deep copy


learningunix:
Even though this might be a little out of topic, your intention is to assign and you need to then really understand the right way of doing it. So whether it be an assignment operator or a copy constructor or in your case - an explicit function written to do an assignment you must be aware of the pitfalls of shallow copying

Have a look here
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35171983
the *mObj = *inObj would do a deep copy if it has correct myClass::operator= defined.

such an operator= would be made automatically by the compiler - if not explicitly made yourself - and in that case would do a member to member copy. such copy is deep copy if none of the members is pointer type or object type without proper operator=. if you defined an operator= yourself you definitively must do a deep copy or the myClass is not well-defined.

the same is true for copy constructor which also always makes deep copy for a well-defined class myClass.

Sara
0
 

Author Closing Comment

by:learningunix
ID: 35311002
Thx
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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

670 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