Solved

object copy

Posted on 2011-03-14
6
297 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

831 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