[ Post New Message | Post Reply to this One | Send Private Email to Mike Mannakee | Help ]

Reference argument to operator =

from Mike Mannakee (B3FEETBACK@aol.com)
In your book THE C++ TRAINING GUIDE, you create a string class as an example. In describing, in chapter 8, why we needed to use a reference argument to the operator = instead of a value argument, you state that the compiler - generated copy constructor will wipe out the caller's pointed-to-data when the temporary is destroyed at the end of its scope.

While this is true, and certainly a danger, isn't this entirely obviated by the fact that we have a perfectly good copy constructor provided in the previous chapter? One that doesn't just copy the pointer itself but also the refered-to-data? I don't see how there would be any danger in that at all as long as a well thought out copy constructor is available. Or would this not be used to create the temporary? Or are you just trying to get us into good programming habits? Or did I just demonstrate that I've missed half the book? I don't fully follow. Please help.

(posted 8248 days ago)

[ Previous | Next ]