newbie q: difference between operator= and copy constructor?

33 views
Skip to first unread message

Harry Potter

unread,
Jan 7, 2013, 2:42:10 PM1/7/13
to cpp-pro...@googlegroups.com
I think the answer to this may solve my previous post: What is the difference between an overloaded = operator and a copy constructor?  How do they behave in ISO C++?

Beyeler Studios

unread,
Jan 8, 2013, 3:58:02 AM1/8/13
to cpp-pro...@googlegroups.com
Hi Harry!


The signatures are:
T(const T& o); // copy constructor
T& operator= (const T& o); // assignment operator

Most notable compiler behaviours are:
T a; // calls default constructor
a = someT; // calls assignment operator
T b(someT); // calls copy constructor
T c = someT; // calls copy constructor, too!

Note: the default behaviour for both copy ctor and assign operator is equal to a memcpy (shallow copy). When you overload one or both in your class, they can basically do whatever. Though it's usually a good idea to either completely emulate the copy & assignment behaviour of basic types (deep copy: no shared memory between objects) or use a different approach to accomplish anything more complex as your code gets very hard to debug when assigning and copying don't behave as expected.

Best,
BeyelerStudios

Harry Potter

unread,
Jan 8, 2013, 9:08:35 AM1/8/13
to cpp-pro...@googlegroups.com

Harry Potter

unread,
Jan 8, 2013, 9:08:48 AM1/8/13
to cpp-pro...@googlegroups.com
Thank you for your help.  I'll look at the site later.  BTW, it seems that, when I pass a class with a char* (I used the new declarator to define a class) and a function to return a modified version of the char* to another function, the member function returns corrupt information for the char*.

Harry Potter

unread,
Jan 9, 2013, 1:24:27 PM1/9/13
to cpp-pro...@googlegroups.com
If I want to pass a class defined using new in one function as a reference to another function, the class becomes corrupt.  Maybe the wrong address is passed.  Or maybe the class is out-of-scope.  When I pass a class as a reference to a function, is there anything I need to know?  I've been able to narrow the bug with my program down to a function call: before the.call, everything work fine,  Within the called function, the class becomes unreadable and member function calls return garbage.

Beyeler Studios

unread,
Jan 11, 2013, 6:40:36 AM1/11/13
to cpp-pro...@googlegroups.com
Hi again!

Can you open up a new thread for this unrelated question and maybe try to give an SSCCE? Check-out http://sscce.org/ on how and why you should do this.

I for one do not understand what you're trying to do from your description alone. As to my confusion, notice how you mix up terms like:
- the definition of a class which is usually the source code of said class
- dynamic memory (allocated using the keyword new) and the concept of passing an object by-reference vs. by-value (passing an object / pointer(?) 'as a reference')
- dynamic memory and variable scope (dynamic memory lasts until you delete the object / array occupying said piece of memory where every variable is said to have a block-scope)
a class running out of scope (again the class is the definition, an object is an instance of a class, and a variable (related to the object) may run out-of-scope - class != object != variable)
I tried giving you some keywords if you want to delve into some tutorials learning these concepts (which I strongly recommend).

Best,
BeyelerStudios
Reply all
Reply to author
Forward
0 new messages