473,383 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

assignment operator question


Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can't do this:

Apple a, b, c;
//Assume there is a function 'Apple operator+(const Apple&, const
Apple&)'

(a + b) = c; //not allowed with second assignment operator, I think.
thanks,
matthew
--

Please remove the word 'tuna' from my name and add a dot j dot ('.j.')
for my correct email address. If you are a spammer please add the word
"Istink" after the word tuna.
Jul 22 '05 #1
9 1680

"Matthew Polder" <ma***************@kodak.com> wrote in message
news:40***************@kodak.com...

Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can't do this:

Apple a, b, c;
//Assume there is a function 'Apple operator+(const Apple&, const
Apple&)'

(a + b) = c; //not allowed with second assignment operator, I think.


No this would be allowed by either form of the assignment operator, assuming
a valid declaration of operator+.

The difference is this

(a = b) = c

With the second form (a = b) returns a const reference which cannot then go
on the left hand side of another operator= (because operator= is not a const
method).

(a = b) = c is pretty useless for built in types like int, but it is legal,
so why not make the same thing legal for user defined classes as well?

john
Jul 22 '05 #2

"Matthew Polder" <ma***************@kodak.com> wrote in message
news:40***************@kodak.com...

Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can't do this:

Apple a, b, c;
//Assume there is a function 'Apple operator+(const Apple&, const
Apple&)'


Assignment operator groups right to left.
a=b=c=d, i would expect a and d to have same values. Was it possible if return
type was const Apple& ?
Jul 22 '05 #3

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2j*************@uni-berlin.de...


a=b=c=d, i would expect a and d to have same values. Was it possible if return
type was const Apple& ?


Sorry, actually it should be ((a=b)=c)=d.

Jul 22 '05 #4
Matthew Polder posted:

Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can't do this:

Apple a, b, c;
//Assume there is a function 'Apple operator+(const Apple&, const
Apple&)'

(a + b) = c; //not allowed with second assignment operator, I think.
thanks,
matthew

Think about that for a second. If it returned a const reference, ie. the
object itself is const, then you wouldn't be allowed use the assignment
operator in the first place!
-JKop
Jul 22 '05 #5
JKop wrote in news:YK*****************@news.indigo.ie in comp.lang.c++:

Matthew Polder posted:
Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)


Think about that for a second. If it returned a const reference, ie. the
object itself is const, then you wouldn't be allowed use the assignment
operator in the first place!


You (and possibly the OP) appear to be confusing a member function
returning a reference to const and a member function that is const.

struct X
{
X const &a() { return *this; }
X &b() const
{
static X x;
return x;
}
};

Above a() can be called only on non-const X's, b() can be
called on both.

Its very common that const member functions return constants
and that non-const member functions return non-constants.

But it isn't required.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6
On Fri, 18 Jun 2004 19:28:52 +0530 in comp.lang.c++, "Sharad Kala"
<no*****************@yahoo.com> wrote,

"Sharad Kala" <no*****************@yahoo.com> wrote in message
news:2j*************@uni-berlin.de...


a=b=c=d, i would expect a and d to have same values. Was it possible if return
type was const Apple& ?


Sorry, actually it should be ((a=b)=c)=d.


That is a bad thing to write; many readers will fail to understand it,
making your code expensive to maintain. If that sequence is your intent
then write it as three statements:
a=b;
a=c;
a=d;

Jul 22 '05 #7
On Fri, 18 Jun 2004 15:20:24 GMT in comp.lang.c++, JKop <NU**@NULL.NULL>
wrote,
Think about that for a second. If it returned a const reference, ie. the
object itself is const, then you wouldn't be allowed use the assignment
operator in the first place!


Eh? Forming a const reference to an object doesn't suddenly make the
object const. Most of the time the return type is not all that
important, since the return value of operator= is often unused.

Jul 22 '05 #8

"David Harmon" <so****@netcom.com.invalid> wrote in message
news:40***************@news.west.earthlink.net...
On Fri, 18 Jun 2004 19:28:52 +0530 in comp.lang.c++, "Sharad Kala"
<no*****************@yahoo.com> wrote,
a=b=c=d, i would expect a and d to have same values. Was it possible if return type was const Apple& ?


Sorry, actually it should be ((a=b)=c)=d.


That is a bad thing to write; many readers will fail to understand it,


Ofcourse, I am not recommending someone to write such a thing.
Jul 22 '05 #9
On Fri, 18 Jun 2004 14:57:12 +0100, "John Harrison"
<jo*************@hotmail.com> wrote in comp.lang.c++:

"Matthew Polder" <ma***************@kodak.com> wrote in message
news:40***************@kodak.com...

Hi,

When a class Apple is written and the assignment operator is not
explicitly declared, the operator

Apple& operator=(const Apple&)

is created by the compiler. Is there any difference between this and

const Apple& operator=(const Apple&)

other than the second form means that you can't do this:

Apple a, b, c;
//Assume there is a function 'Apple operator+(const Apple&, const
Apple&)'

(a + b) = c; //not allowed with second assignment operator, I think.


No this would be allowed by either form of the assignment operator, assuming
a valid declaration of operator+.

The difference is this

(a = b) = c

With the second form (a = b) returns a const reference which cannot then go
on the left hand side of another operator= (because operator= is not a const
method).

(a = b) = c is pretty useless for built in types like int, but it is legal,
so why not make the same thing legal for user defined classes as well?

john


Given:

int a, b = 2, c = 3;

....then:

(a = b) = c;

....is syntactically legal in C++ (not in C), but perhaps it should not
be. Since the statement modifies the value of a twice without an
intervening sequence point, it produces undefined behavior.

I understand the purpose of changing the return type of the assignment
operators from rvalue (as they are in C) to lvalue, for use with user
defined types. There the assignment operator has the semantics of a
function, and there are sequence points.

There is no way to use the fact that assignment to a built-int
evaluates to an lvalue result other than using the & operator on it
without invoking undefined behavior. You can do this in C++, but not
in C:

#include <iostream>

void func(int *q)
{
++*q;
}

int main()
{
int x;
func(x = 3);
std::cout << "x = " x "\n";
return 0;
}

....and the output will be 4.

In fact this leads to the mess in C++ with volatile values. Given:

volatile int x;
int y; /* not volatile */

y = x = 3; /* equivalent to y = (x = 3) */

The first assignment, 'x = 3' produces not the integer value 3, but
the address of x. The C++ standard requires the compiler perform
lvalue to rvalue conversion, and it most certainly cannot optimize it
away with x being volatile.

In fact, it is at least arguable that:

x = 3;

....requires lvalue to rvalue conversion (reading x after writing),
because even though the rvalue is not used, the semantics of the
volatile type qualifier require it.

And think of memory-mapped hardware devices (the sort of thing one
uses volatile for) like UARTs where the transmit register is
write-only, the receive register is read-only, and they both occupy
the same address...

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
5
by: CoolPint | last post by:
It seems to me that I cannot assign objects of a class which has a constant data member since the data member cannot be changed once the constructor calls are completed. Is this the way it is meant...
16
by: Edward Diener | last post by:
Is there a way to override the default processing of the assignment operator for one's own __value types ? I realize I can program my own Assign method, and provide that for end-users of my class,...
1
by: Tony Johansson | last post by:
This class template and main works perfectly fine. But could be better. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter...
10
by: Christian Christmann | last post by:
Hi, how do I define an assignment operator which is supposed to copy all member attributes of one object to another where both objects are given as pointers? Example: CLASS_A *source = new...
13
by: Daniel W | last post by:
Hi! I tried to post this to comp.lang.c.moderated but it didn't seem to go through. I've got a question about volatiles in assignment expressions. I found the following code snippet in an...
19
by: scroopy | last post by:
Is it impossible in C++ to create an assignment operator for classes with const data? I want to do something like this class MyClass { const int m_iValue; public: MyClass(int...
2
by: sven.bauer | last post by:
Hi, I have a question following up the following slightly older posting: http://groups.google.de/group/comp.lang.c++/browse_thread/thread/40e52371e89806ae/52a3a6551f84d38b class Base {...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.