CSmth & CSmth::operator = (const CSmth & rhs)
{
return *this;
}
-
3Do you have a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Benjamin Lindley Jul 11 '11 at 20:54
-
It means it's probably bad code. If you've got members to copy, this function should do more. If you don't, this function shouldn't exist. – GManNickG Jul 11 '11 at 21:06
-
i'm c# oriented, need to understand several rows in unmanaged code ASAP. – Jul 11 '11 at 21:36
-
@GMan: About all I can think of is a Borg Pattern object (so nothing to copy) with a reference member to shared state (so no default copy assignment operator). It'd be some bad luck for that to be the first assignment operator the questioner ever saw. – Steve Jessop Jul 11 '11 at 21:42
-
So many feedbacks.. stackoverflow is very good place... thanks to everyone.. – Jul 11 '11 at 22:05
3 Answers
This is an assignment operator, so you can write:
CSmth a;
CSmth b;
a = b;
The actual implementation does nothing useful, apart from returning a reference to the first addendum. A more standard implementation would be:
CSmth & CSmth::operator = (const CSmth & rhs) {
if (this != &rhs) // protect against invalid self-assignment
{
do_whatever_required_to_copy_rsh_on_to_this;
}
return *this;
}

- 68,819
- 11
- 102
- 123
It's an assignment operator, used to assign values from the rhs
object to the current (this
). It hasn't been implemented, however.

- 120,358
- 21
- 212
- 242
operator= is the 'Copy Assignent Operator'. Any time you see
A a, b;
a = b;
What really happens in the simplest case is this:
A & A::operator=(A const &rhs);
a.operator=(b);
To break this down:
- operator= is a member of A. The operator has access to all of the variables in the object the operator is being called upon, aka the left hand side variable.
- operator= takes as a parameter a reference to another A, the right hand side of the assignment. Good coding practice states you should only access the public interface of rhs from within this function. **
- operator= returns a reference to an A, by convention this is to the left hand side of the assignment, or
*this
. This return is important because it allows you to chain assignments like so:A a, b, c, d; a = b = c= d;
A call to operator= should "copy" the state of the right hand side object into the left hand side object. Because it looks like we're doing an assignment, this is called the "Copy Assignment Operator"
** Denoting a variable as 'private' makes that variable private to that class, not to that object. You can fully access the private implementation of the passed object from within a copy assignment operator but this isn't a good idea as it violates object encapsulation, among other things.

- 1,885
- 1
- 12
- 13
-
I disagree with your ** footnote since we're talking about the same class, `A`. If you can't you assume that `class A`'s assignment operator knows how to deal with `class A` private members, then what can you assume? – Chris A. Jul 11 '11 at 21:41