[comp.lang.c++] Lvalue checking for operator= ??

deb@svax.cs.cornell.edu (David Baraff) (10/22/89)

Suppose you have a standard matrix class, which implements '+',
and also has its own assignment operator defined i.e.

	matrix &matrix::operator= (matrix &m)

exists. Then,

	matrix	a, b, c;

		...

	a+b = c;

appears to be legal. (As least it compiled under 1.2.) Is it legal under
2.0 ? What does it really mean? Shouldn't  the '=' operator be forced
to only accept an lvalue as its left-hand operand?

	David Baraff
	deb@svax.cs.cornell.edu

ark@alice.UUCP (Andrew Koenig) (10/23/89)

In article <33433@cornell.UUCP>, deb@svax.cs.cornell.edu (David Baraff) writes:
> Suppose you have a standard matrix class, which implements '+',
> and also has its own assignment operator defined i.e.

> 	matrix &matrix::operator= (matrix &m)

> exists. Then,

> 	matrix	a, b, c;

> 		...

> 	a+b = c;

> appears to be legal. (As least it compiled under 1.2.) Is it legal under
> 2.0 ? What does it really mean? Shouldn't  the '=' operator be forced
> to only accept an lvalue as its left-hand operand?

How about making operator+ return a const matrix?
Then you won't be able to assign to it.


To tell the truth, I hadn't thought about this issue until this
question forced me to do so.  There are zillions of things like
string classes out there that say

	extern String operator+(const String&, const String&);

and apparently they really should say

	extern const String operator+(const string&, const String&);

The point is that unless you say otherwise, you can't call operator=
on behalf of a const object (well, 2.0 will let you get away with
it but will give a warning).  I think that's almost exactly
what you want.
-- 
				--Andrew Koenig
				  ark@europa.att.com

peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) (10/24/89)

In article <10045@alice.UUCP> ark@alice.UUCP (Andrew Koenig) writes:
>In article <33433@cornell.UUCP>, deb@svax.cs.cornell.edu (David Baraff)
writes:
>How about making operator+ return a const matrix?
>Then you won't be able to assign to it.
>
> [...]
>on behalf of a const object (well, 2.0 will let you get away with
>it but will give a warning).  I think that's almost exactly
>what you want.
>				--Andrew Koenig

Which brings up an interesting point. How do I tell the compiler that `*this'
is const with respect to some operation?

After all that is really what I need in the above case?!

Peter
peter@media-lab.media.mit.edu

ark@alice.UUCP (Andrew Koenig) (10/24/89)

In article <874@mit-amt.MEDIA.MIT.EDU>, peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) writes:

> How do I tell the compiler that `*this'
> is const with respect to some operation?

> After all that is really what I need in the above case?!

You put `const' in the appropriate place in the member
function declaration.  For example:

	class Foo {
	public:
		int getsize() const;
		void setsize(int);
		// ...
	};

	int Foo::getsize() const
	{
		int x;
		// ...
		return x;
	}

	void Foo::setsize(int x)
	{
		// ...
	}

Here, the instances of `const' in Foo::getsize() say that
*this is a const inside getsize but not in setsize.  Thus:

	main()
	{
		Foo f;
		const Foo cf;

		f.setsize(3);			// OK
		int n = f.getsize();		// OK
		n = cf.getsize();		// OK
		cf.setsize(7);			// error -- cf is const
	}
-- 
				--Andrew Koenig
				  ark@europa.att.com