Friday, January 11, 2013

[Cpp-Programming] Re: newbie q: difference between operator= and copy constructor?

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

Am Mittwoch, 9. Januar 2013 19:24:27 UTC+1 schrieb Harry Potter:
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.

On Tuesday, January 8, 2013 9:08:48 AM UTC-5, Harry Potter wrote:
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*.

On Tuesday, January 8, 2013 3:58:02 AM UTC-5, Beyeler Studios wrote:
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

On Monday, 7 January 2013 20:42:10 UTC+1, Harry Potter wrote:
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++?

--
You received this message because you are subscribed to the Google Groups "C++ Programming" group.
To view this discussion on the web visit https://groups.google.com/d/msg/cpp-programming/-/FkVTTAX10Y0J.
To post to this group, send email to cpp-programming@googlegroups.com.
To unsubscribe from this group, send email to cpp-programming+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cpp-programming?hl=en.

0 Comments:

Post a Comment

<< Home