Solved

object copy

Posted on 2011-03-14
6
296 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
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 33

Accepted Solution

by:
sarabande earned 250 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
Quicksort a dynamic deque 33 66
Fast access to array, using indexes, smart iterative search 2 107
Path to  STL Map header file 1 41
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now