Linked Questions
13 questions linked to/from Why should the assignment operator return a reference to the object?
11 votes
2 answers
10k views
Assignment operator overloading: returning void versus returning reference parameter [duplicate]
Some of the assignment overloading operator examples I see online look like this: #include <iostream> using namespace std; class Distance { private: int feet; // 0 to ...
1 vote
3 answers
163 views
operator overloading [duplicate]
iarray<T>& operator = (iarray<T>& v) Why the return type is iarray<T>& not iarray<T> ? UPDATE Can someone elaborate in great detail why iarray<T> const &...
1 vote
1 answer
110 views
C++ : In Class Assignment operator function why do we have to return *this [duplicate]
I have written a class in C++ and have defined assignment operator function: cplusplus.com recommends following syntax. check operator= (const check& obj){*cstr = *(obj.cstr); return *this;} One ...
-2 votes
2 answers
195 views
Class method returning reference C++/SystemC [duplicate]
To pass user-defined datatypes into SystemC channels templates requires these datatypes to be defined as a class that implements also different kind of operators <<, =, ==. I need to define a ...
0 votes
0 answers
110 views
"void" Return type for overloaded operator [duplicate]
I am currently studying "Object-oriented design choices" by Dingle (2021), a required textbook for my Object-Oriented Design Class. While going through the chapter on "Move Semantics,&...
0 votes
0 answers
42 views
Why does a function or method have to return a reference in order to be chained? [duplicate]
I've been brushing up on C++ in preparation for a class, and something that's stumped me is returning a reference vs. returning a value (also vs. returning a const reference, but that's not really ...
2419 votes
5 answers
536k views
What is the copy-and-swap idiom?
What is the copy-and-swap idiom and when should it be used? What problems does it solve? Does it change for C++11? Related: What are your favorite C++ Coding Style idioms: Copy-swap Copy constructor ...
5 votes
3 answers
2k views
assignment operator return a reference to *this in C++
I read about this from "Effective c++" ,this is Col.10. It say it's a good way to have assignment operators return a reference to *this. I wrote a code snippet to test this idea. I overridden the ...
5 votes
2 answers
14k views
Operator overloading C++ reference or value
I've seen many tutorials and tried to find the answer on stackoverflow but with no success. What I'm not sure of is; is there some praxis when to return by value or by reference, when overloading an ...
4 votes
4 answers
473 views
Returning *this with an assignment operator
I went over making my own copy constructor and it overall makes sense to me. However, on the topic of doing your own assignment operator I need someone to fill in the blank for me. I pretty much don'...
0 votes
3 answers
895 views
C++:copy constructor
I have a problem with my program in c++. I try to write copy constructor. Everything is good until I start copying elements from array from object that is applied as a reference. I can print every ...
1 vote
4 answers
163 views
Why it is required to return *this when this is still being passed?
I have written following class which has overloaded assignment operator. As shown in example everywhere I returned *this from assignment operator. class Sample { int *p; int q; public: ...
2 votes
0 answers
430 views
Overloading operators, multiple assignment e.g. a=b=c
I am having trouble finding a way to enable the assignment operator to do multiple assignments. given that there is item A,B,C; for(int i=0; i<10; i++) C.AddHead(i); A = B = C; for a single ...