Skip to contentSkip to conversation

operator=

DISCUSSION Shriramana Sharma 2 participants 2 messages Jul 10, 2007 – Jul 11, 2007

lore
Share & tools
lore
#0 lore raw

As Glynn pointed out, this list is about C on Linux (while obviously C doesn't exclude C++), but still as I do not find as good a *mailing list* elsewhere on the net, I beg your forgiveness and will continue to ask just a few general questions now and then to which I have already *tried* otherwise to get answers.

Are there different kinds of operator=? I read this somewhere: "copy-assignment operator=" and "other forms of operator=".

Shriramana Sharma.

#1 lore raw
Quoted 2 lines · Shriramana Sharma

Are there different kinds of operator=? I read this somewhere:
"copy-assignment operator=" and "other forms of operator=".

I suspect that you're referring to the fact that initialisation can be differentiated from assignment to an existing object. E.g. if you have:

	class foo
	{
		foo(int);
		foo(const foo&);
		foo& operator=(const foo&);
	}
	
	void f()
	{
		foo f1(10);
		foo f2 = f1;
	}

The initialisation of f2 with:

	foo f2 = f1;

is equivalent to:

	foo f2(f1);

and NOT to:

	foo f2;
	f2 = f1;

IOW, it uses the copy constructor foo(const foo&), not operator=.

If you don't have a copy constructor, operator= will be used instead. If you don't have either, the compiler will just perform a bitwise copy, as for copying C structures.

Also, note that implicit initialisation of function arguments and return values also uses the copy constructor if it exists.

Thus, if you define both a copy constructor and operator=, the copy constructor will be applied to uninitialised objects while operator= will only be applied to initialised objects.

This allows operator= to refer to the existing state of the object being modified, as it will always be initialised. This is useful if an object contains dynamically-allocated values; when you change the value by assignment, you may want to de-allocate existing values and/or duplicate new values.

E.g.

	class string
	{
		char *buf;

		string(char *p) {
			buf = strdup(p);
		}
		string(const string &s) {
			buf = strdup(s.buf);
		}
		string& operator=(const string &s) {
			free(buf);
			buf = strdup(s.buf);
		}
	}
Signature · 1 line
Glynn Clements <[email protected]>