473,564 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloaded assignment operator

I am going through Deitel & Deitel's C++ book (section 8.8 of the
fourth edition), in which they construct an Array class and show how
to overload operators. The assignment operator is overloaded as
follows:

const Array &operator=(cons t Array &);

According to D&D, the const return is designed to avoid (a1 = a2) =
a3. My questions are:

1) Why is this necessary? After all, an assignment like (a1 = a2) = a3
works for ordinary variables.

2) What if you want to use this method on a non-constant Array object,
or if you want it to return a non-constant Array? I can see it still
works, but why don't the const declarations get in the way?

Thanks.

Oct 18 '07 #1
2 2485
Bruno Panetta wrote:
I am going through Deitel & Deitel's C++ book (section 8.8 of the
fourth edition), in which they construct an Array class and show how
to overload operators. The assignment operator is overloaded as
follows:

const Array &operator=(cons t Array &);

According to D&D, the const return is designed to avoid (a1 = a2) =
a3. My questions are:

1) Why is this necessary? After all, an assignment like (a1 = a2) = a3
works for ordinary variables.
It is not necessary. It's a design decision, and a debatable one. Note that
the standard describes the Assignable requirement in Table 64 as follows:
the expression t=u has to have the postcondition that t be equivalent to u
and the return type T&. It follows that classes whose assignment operator
returns a const reference are not assignable. Consequently, something like

std::vector< Array >

is not required to compile. (Although in practice, only the most zealous
concept checking libraries will snap at that.)
Also note that the standard containers all have an assignment operator that
return a non-const reference (in fact, this is a container requirement). I
don't see a good reason to depart from this pattern.

2) What if you want to use this method on a non-constant Array object,
No problem.
or if you want it to return a non-constant Array?
Then you should make the return type non-const.
I can see it still works, but why don't the const declarations get in the
way?
Huh?

Please provide a piece of code that illustrates your worries.
Best

Kai-Uwe Bux
Oct 18 '07 #2
On Oct 18, 8:44 am, Kai-Uwe Bux <jkherci...@gmx .netwrote:
Bruno Panetta wrote:
I am going through Deitel & Deitel's C++ book (section 8.8 of the
fourth edition), in which they construct an Array class and show how
to overload operators. The assignment operator is overloaded as
follows:
const Array &operator=(cons t Array &);
According to D&D, the const return is designed to avoid (a1 = a2) =
a3. My questions are:
1) Why is this necessary? After all, an assignment like (a1 = a2) =a3
works for ordinary variables.
Note that the above assignment *doesn't* work for basic types or
pointers, but rather has undefined behavior (in C++ -- in C, it
shouldn't compile).
It is not necessary. It's a design decision, and a debatable
one. Note that the standard describes the Assignable
requirement in Table 64 as follows: the expression t=u has to
have the postcondition that t be equivalent to u and the
return type T&.
It's entirely possible that Deitel & Deitel wrote their work
before the STL became generally used. Given the constraints of
the STL, today, I don't think that there's any question to the
fact that the return type should be T&, and not T const&.
Before the STL became wide spread, there was considerable room
for discussion: the return of a const reference is as close as
you can come to similating the C behavior of the built-in types.
And many people don't agree with C++'s loosening of the rules
here. The use of a const reference here was a fairly wide
spread convention in earlier days.
It follows that classes whose assignment operator returns a
const reference are not assignable. Consequently, something
like
std::vector< Array >
is not required to compile. (Although in practice, only the
most zealous concept checking libraries will snap at that.)
One could argue that it is overspecified. IMHO, Assignable
should only require that assignment works as expected, with no
real constraints on the type of the return value (other than
that it can safely be ignored).
Also note that the standard containers all have an assignment
operator that return a non-const reference (in fact, this is a
container requirement). I don't see a good reason to depart
from this pattern.
Not departing from the general pattern is a different argument.
I dropped the const early myself, simply to be compatible with
the built-in types, even though it only affects programming
styles that I don't like to begin with.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 18 '07 #3

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

Similar topics

8
17851
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. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said...
5
6875
by: Andy Jarrell | last post by:
I'm trying to inherit from a specific class that has an overloaded operator. The problem I'm getting is that certain overloaded operators don't seem to come with the inheritance. For example: // TestA.h --------------------------------------- #include <iostream> enum Aval { FIRST_VALUE,
4
1806
by: August1 | last post by:
I've written an interface and implementation file along with a client source file that allows the use of an overloaded subtraction operator. However, when using the program, I'm running into a memory problem that I'm not readily seeing that lies within the overloaded operator - I think pertaining to the temporary class object that is created...
2
1677
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you can see in the constructor. Class Matris should create a matris by using class Intvektor. So what I want to have is a pointer to an array...
2
7357
by: B. Williams | last post by:
I have an assignment for school to Overload the operators << and >and I have written the code, but I have a problem with the insertion string function. I can't get it to recognize the second of number that are input so it selects the values from the default constructor. Can someone assist me with the insertion function. The code is below. ...
5
2281
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 my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler...
5
2268
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the overloaded operator function performs the necessary copy of obj's member in to obj1. Can't the same be done using copy
2
2104
by: subramanian100in | last post by:
overloaded operator=() -------------------------------- overloaded assignment operator should be a non-static MEMBER function of a class. This ensures that the first operand is an lvalue. If the overloaded assignment operator function is allowed to be a non- member function then we may be able to write the following: Suppose we have a...
9
2042
by: itdevries | last post by:
Hi, I've ran into some trouble with an overloaded + operator, maybe someone can give me some hints what to look out for. I've got my own custom vector class, as a part of that I've overloaded the + operator by means of a friend function. Everything worked fine until I decided to use a variable array size (by using new/delete), now I get an...
3
3904
by: jr.freester | last post by:
I have created to classes Matrix and System. System is made up of type matrix. ---------------------------------------------------------------------------------- class Matrix { private: int row, col; double *data public: Matrix(const int& M, const int& N): row(M), col(N)
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.