Re: operator=

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Shriramana Sharma wrote:

> 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);
		}
	}

-- 
Glynn Clements <glynn@xxxxxxxxxxxxxxxxxx>
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux