Solved

object copy

Posted on 2011-03-14
6
300 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 34

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 34

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

623 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