473,385 Members | 1,940 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,385 software developers and data experts.

A doubt on assignment operator.

Hi,
I am trying to define the assignment operator for a class called
Cell. I am bit confused between the following two statements:

const Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff

}

and,

Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff

}

Is the first work going to work at all? does it make any sense? I think
it is not going to do what it is intended to do.

thanks,
--a.

Sep 26 '05 #1
4 1238
On 26 Sep 2005 11:55:12 -0700
"Amit Bhatia" <am*********@gmail.com> wrote:
Is the first work going to work at all?
Try it out for yourself.
does it make any sense?
Well, let's try and figure out what the definition const Cell&
Cell::operator=(const Cell &r) actually means: It says that the
assignment operator shall return a constant reference of type Cell.
It's usually of no great importance whether you have both a const and
non-const assignment operator -- you will bearly use the object
returned by it.

It does however make sense for an (merely used) if-statement such as
if((Cell1=Cell2).changeContent("a"))
when you don't want Cell1's content to be changed.
I think it is not going to do what it is intended to do.


You don't mention what you intend to do, hence I can't tell you. FYI:
Cell& Cell::operator=(const Cell &r) const is certainly rubbish.

best regards / Gruß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpg http://gpg.notlong.com
Sep 26 '05 #2
Amit Bhatia wrote:
I am trying to define the assignment operator for a class called
Cell. I am bit confused between the following two statements:

const Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff

}

and,

Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff

}

Is the first work going to work at all? does it make any sense? I think
it is not going to do what it is intended to do.


The difference between the two is the return value type, right? The
return value type is different only in the const-ness of the object to
which the returned reference points, right? So, why would the first
one *not work*? When you write the expression

someCell = someOtherCell;

it's the same as

someCell.operator=(someOtherCell);

which basically _discards_ the return value. Whatever happens between
passing the argument and returning a value, IOW, whatever happens between
the curly braces of the function, is the same, no? So, how it may not
work if they are doing exactly the same?

V
Sep 26 '05 #3
On Mon, 26 Sep 2005 21:13:05 +0200
Moritz Beller <mo*****************@t-online.de> wrote:
It's usually of no great importance whether you have both a const and xx --> or non-const assignment operator -- you will bearly use the object


Who put the "both ... and" in there? ;)

best regards / Gruß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpg http://gpg.notlong.com
Sep 26 '05 #4

Victor Bazarov wrote:
Amit Bhatia wrote:
I am trying to define the assignment operator for a class called
Cell. I am bit confused between the following two statements:

const Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff

}

and,

Cell& Cell::operator=(const Cell &r)
{
\\ all the stuff

}

Is the first work going to work at all? does it make any sense? I think
it is not going to do what it is intended to do.


The difference between the two is the return value type, right? The
return value type is different only in the const-ness of the object to
which the returned reference points, right? So, why would the first
one *not work*? When you write the expression

someCell = someOtherCell;

it's the same as

someCell.operator=(someOtherCell);

which basically _discards_ the return value. Whatever happens between
passing the argument and returning a value, IOW, whatever happens between
the curly braces of the function, is the same, no? So, how it may not
work if they are doing exactly the same?

V


The original poster probably can't tell whether either function works
because they won't compile. The two functions differ only by their
return type - making one an illegal overload of the other if both are
declared.

Greg

Sep 27 '05 #5

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,...
9
by: Rick N. Backer | last post by:
I have an abstract base class that has char* members. Is an assignment operator necessary for this abstract base class? Why or why not? Thanks in advance. Ken Wilson Amer. Dlx. Tele,...
1
by: Jon Slaughter | last post by:
I have a chain of classes(i.e., a series of classes each containing an array of the next class). Each class has array like access. struct Myclass1 { vector(Myclass2) _Myclass2; Myclass2&...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
6
by: krishna | last post by:
Hi , When i read linux tcp/ip stack codes i come cross lot of structure assignment like this. I guess '=' is the assignment operator and is there any particular reason for using ':' as the...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.