Home > Back-end >  The assignment operator in the return value with const and don't take what is the difference be
The assignment operator in the return value with const and don't take what is the difference be

Time:02-28

When overloaded assignment operator
1 - const intlong & amp; Operator=(const intlong&)
2 - intlong & amp; Operator=(const intlong&)
I thought the first return value cannot be modified with const said, only as a constant returns, but tests found and the second is the same, can be used for continuous assignment
For example,
Intlong a (5);
Intlong b (0);
Intlong c (6);
A=b=c;
- the return value is 1 const and 2 kinds of return value without a const what's the difference?

There are
3 - intlong & amp; Operator=(const intlong&) Const;
4 - intlong operator=(const intlong&) Const;
The two defined as const overloaded assignment operator have what effect?
- do not modify this internal members how to change the value of this assignment? . Does that mean overloaded assignment operator in addition to meaningful 1 and 2, 3 and 4 meaningless

#include
using namespace std;
The class intlong {
Friend ostream & amp; Operator<(ostream& , const intlong&);
Public:
Intlong (int);
Intlong (const intlong&);
Intlong operator + (const intlong&) Const;
Const intlong & amp; Operator=(const intlong&);//adding const
//intlong & amp; Operator=(const intlong&);//the front without const cons and how cannot define a const class

Private:
Int p;

};


Const intlong & amp; Intlong: : operator=(const intlong& RHS) {
This - & gt; P=RHS. P;
Return * this;
}
/*
Intlong & amp; Intlong: : operator=(const intlong& RHS) {
This - & gt; P=RHS. P;
Return * this;
} */

Ostream & amp; Operator<(ostream & amp; Cout, const intlong & amp; RHS) {
CoutReturn cout.
}

Intlong: : intlong (int RHS) : p (RHS) {
}
Intlong: : intlong (const intlong & amp; RHS) {
This - & gt; P=RHS. P;
}
Intlong intlong: : operator + (const intlong & amp; RHS) const {
Intlong temp (0);
Temp. P=(* this). P + RHS. P;
Return temp.
}

Int main ()
{
Intlong a (5);
Intlong b (6);
Intlong c (7);
A=b=c;

Coutreturn 0;
}

CodePudding user response:

The return value is the right value, const modifiers rvalue doesn't make any sense

CodePudding user response:

T a=b in this case, whether b const, replication, can occur in a
A simple example
int a=1; 1//here is obviously a const int because it is a literal, cannot be rewritten, execute the statement after 1 this data will be copied into a storage unit of the

CodePudding user response:

The T operator=(.. ) const is the assignment operator error, because the assignment will be to modify, and const is not allowed to change, so wrong

CodePudding user response:

refer to the second floor truth is right or wrong response:
T=b in this case, whether b const, replication, can occur in a
A simple example
int a=1; 1//here is obviously a const int because it is a literal, cannot be rewritten, execute the statement after 1 this data will be copied into the storage unit of a


May not be the reason, int a=1 even const int a=1 this is possible, because this is the initialization is not the assignment, written in the book have said const is works after initialization,

1 - const intlong & amp; Operator=(const intlong&)
2 - intlong & amp; Operator=(const intlong&)
3 - intlong & amp; Operator=(const intlong&) Const;
4 - intlong operator=(const intlong&) Const;
I want to ask, isn't 1 and 2 overloaded assignment operation FuCai meaningful, 3, and 4 although can compile and actually useless

CodePudding user response:

The same,
int a=10;
A=1;//here is the initialization or assignment??
Actually your problem is not difficult, but want you to understand is difficult, suggest see ec++
3, 4, unless you don't modify this, as long as you a change, will go wrong,

CodePudding user response:

I am still that sentence, fragmentation is not recommended to study, find a book to learn the best system, otherwise easy to elephant

CodePudding user response:

The assignment must be modified, so is wrong

CodePudding user response:

refer to 7th floor truth is right or wrong response:
assignment must be modified, so the 34 is wrong


3 and 4 can compile is corresponding to const objects such as
Const intlong d (8);
D=a;
This kind of situation if we do not offer 3 or 4 will be compiled in the past, not later want to along while also provide 3 and 4 can't figure out how to modify think 3, and 4 d object seems meaningless

CodePudding user response:

Const intlong d (8);
D=a;
What do you think of this code right?

CodePudding user response:

If the code right,

Const int a=10;
A=11;
This code should be right

CodePudding user response:

refer to 6th floor truth is right or wrong response:
I am still that sentence, fragmentation is not recommended to study, find a book to learn the best system, otherwise easy to elephant


Thank you, looking at the university of CPP tutorial fifth edition of the book is in the process of learning writing and that writing can be compiled, what's the use of this kind of question
  • Related