Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Overloaded assignment operator

Posted on 2007-10-17
12
Medium Priority
?
212 Views
Last Modified: 2010-04-01
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=(const 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.
0
Comment
Question by:Rothbard
  • 3
  • 3
  • 2
  • +2
12 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 20097079
Scott Meyers would disagree with D&D.

Scott Meyers is a well known, and respected C++ author.

0
 
LVL 86

Expert Comment

by:jkr
ID: 20097111
>>Why is this necessary?

Actually, I'd say it isn't. Check the STL - you wil hardly find any assignment operator in there that returns a 'const' reference. Maybe that's personal style.
0
 
LVL 30

Expert Comment

by:Axter
ID: 20097219
>>Maybe that's personal style.

Early C++ books recommended the D&D format.
However, more modern C++ books recommend not using const return type, and to make the object compatible with the standard C++ POD types and STD objects.

So the D&D book is pushing an out dated method.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:jkr
ID: 20098286
IMO not only outdated, but also unnecessary, if I might add.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 320 total points
ID: 20099214
>> 1) Why is this necessary? After all, an assignment like (a1 = a2) = a3 works for ordinary variables.

I guess he wants his assignments to happen like a1 = (a2 = a3) only. If you think about it, it doesn't make sense to want to do (a1 = a2) = a3, because it's equivalent to :

        a1 = a2;
        a1 = a3;

The first assignment is unnecessary, so you might as well have just used :

        a1 = a3;


>> I can see it still works, but why don't the const declarations get in the way?

The const qualifier for the parameter is basically a promise that the assignment operator will not modify the value. It does not mean that you can only pass it const objects. You can still pass it a non-const object, but the assignment operator will treat it as if it were a const object :

        Array a;
        const Array b;
        Array c;
        a = b;        // <--- this is ok
        a = c;        // <--- and this is too

However, you cannot use an assignment operator on a const object, because that doesn't make sense :

        const Array a;
        Array b;
        a = b;        // <---- what ??? You can't modify a const object
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 180 total points
ID: 20099341
>>>> If you think about it, it doesn't make sense to want to
>>>> do (a1 = a2) = a3, because it's equivalent to :
>>>>      a1 = a2;
>>>>      a1 = a3;

Actually, it is only the same if we assume an assignment would create identical copies. If - for example - an assignment would include some kind of versioning, it is not the same.

Generally, I would agree to Axter's opinion. Returning a const reference is a worthless restriction, a chicane. A container class is a helper and a helper should not limit its usability for nothing. With D&D the following would not work

  int writexml(Array<XMLToken>& tokens)
  {
        XMLDecl xmldecl(...);
        tokens.insertAt(0, xmldecl);
        ...
  }

    Array<XMLToken> from, to;
    fillxml(from);
    writexml(to = from);

and I could see any reason for it.

Regards, Alex
   
   


0
 

Author Comment

by:Rothbard
ID: 20099349
What about the "const" return type? I.e. what is the difference between having

const Array &operator=(const Array &);

and

Array &operator=(const Array &);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20099387
>> If - for example - an assignment would include some kind of versioning, it is not the same.

imo, an assignment should be atomic, and not impact another assignment. But you're right to mention it of course.


>> What about the "const" return type? I.e. what is the difference between having
>> 
>> const Array &operator=(const Array &);
>> 
>> and
>> 
>> Array &operator=(const Array &);

The difference is what was described : it doesn't allow (a1 = a2) = a3. Because the first assignment (a1 = a2) would be const, and can't be assigned to. The compiler will complain about that, and won't allow you to use the code.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20099412
>>>> what is the difference between having ... const ... and non-const

A const return value cannot passed as a non-const reference to a function.

Look at my example:

        writexml(to = from);

That code only compiles if the return of operator= is a non-const reference (of the 'to' array).

Also code like

    (a = b) += c;

would not work, which was supposed to firstly make a copy of 'b' *before* appending the c. You could help yourself by

     a = b;
     a += c;

but I neither think that the second is much clearer nor that there is any advantage in restricting the usage of temporary results. If you see the terms as math operands you of course expect terms like (a += b) can be used at both sides of an assignement.
0
 

Author Comment

by:Rothbard
ID: 20099430
Thanks for your replies!
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 20099439
>>>> imo, an assignment should be atomic,
I had a system where each single object had a 'unique' object id, which additionally was used as a primary key  in a database. Therefore you never could make perfect copies but always had to add a new object id after an assignment. Or take a string class with reference counting. An assignment would copy the pointer to the internal string object and increment the reference counter. Of course that operation cannot be atomic.

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20099900
My choice of the word "atomic" might have been misleading. What I meant is that this :

        (a = b) = c;

should not have a different result than this :

        a = c;

(there are always exceptions of course).

In your example, when you assign, you have to create a new object in the database, but you erase the old one, no ? So, the end result is the same.

Anyway : it doesn't really matter, because using const for the return type of the assignment operator limits too much (as you've shown in your examples). So, it's not a good idea, unless you WANT those limitations.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question