linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* operator=
@ 2007-07-10  5:00 Shriramana Sharma
  2007-07-11 15:10 ` operator= Glynn Clements
  0 siblings, 1 reply; 2+ messages in thread
From: Shriramana Sharma @ 2007-07-10  5:00 UTC (permalink / raw)
  To: Linux C Programming List

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.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: operator=
  2007-07-10  5:00 operator= Shriramana Sharma
@ 2007-07-11 15:10 ` Glynn Clements
  0 siblings, 0 replies; 2+ messages in thread
From: Glynn Clements @ 2007-07-11 15:10 UTC (permalink / raw)
  To: Shriramana Sharma; +Cc: Linux C Programming List


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@gclements.plus.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2007-07-11 15:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-10  5:00 operator= Shriramana Sharma
2007-07-11 15:10 ` operator= Glynn Clements

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).