Solved

object copy

Posted on 2011-03-14
6
295 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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 32

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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++.

762 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

16 Experts available now in Live!

Get 1:1 Help Now