operator= understanding problem
-
Dear all, I am having a problem in understanding operator overloading in C++. Below is the code I am reading and its explanation. There are some points that I do not really understand. [CODE] class Ratio { public: Ratio(int =0, int =1); //default constructor Ration(const Ratio&); //copy constructor Ration& operator=(const Ratio&); //assignment operator private: int num, den; }; Ratio& Ratio::operator=(const Ratio& r) { num = r.num; den = r.den; return *this; } [/CODE] In the book it is written: [QUOTE] The assignment operator is a function that should return the value it assigns.(This is okay) The assignment operator should return a reference to the same type as the object being assigned. The return type is a reference to an object of the same class. ( I would say okay for both statements) But then this means that the function should return the object that is being assigned in order for the assignment chain to work. So when the assignment operator is being overloaded as a class member function, it should return the object that owns the call. Since there is no other name available for this owner object, C++ defines a special pointer, named this pointer, which points to the owner object. [/QUOTE] I do not really ubderstand the rest of the statements about the object that owns the call. Also I do not understand the implementation code of the operator= function. Why do we use the r object to access the member data and then assign them to the same member data? I do not understand the logic behind that piece of code. Regards, grscot
-
Dear all, I am having a problem in understanding operator overloading in C++. Below is the code I am reading and its explanation. There are some points that I do not really understand. [CODE] class Ratio { public: Ratio(int =0, int =1); //default constructor Ration(const Ratio&); //copy constructor Ration& operator=(const Ratio&); //assignment operator private: int num, den; }; Ratio& Ratio::operator=(const Ratio& r) { num = r.num; den = r.den; return *this; } [/CODE] In the book it is written: [QUOTE] The assignment operator is a function that should return the value it assigns.(This is okay) The assignment operator should return a reference to the same type as the object being assigned. The return type is a reference to an object of the same class. ( I would say okay for both statements) But then this means that the function should return the object that is being assigned in order for the assignment chain to work. So when the assignment operator is being overloaded as a class member function, it should return the object that owns the call. Since there is no other name available for this owner object, C++ defines a special pointer, named this pointer, which points to the owner object. [/QUOTE] I do not really ubderstand the rest of the statements about the object that owns the call. Also I do not understand the implementation code of the operator= function. Why do we use the r object to access the member data and then assign them to the same member data? I do not understand the logic behind that piece of code. Regards, grscot
Ratio r1, r2; r1 = r2; is equal to: r1.operator=(r2); I guess what the author means is that r1 "owns" the .operator=()-call. But how do you return the r1-object? This is where you need the this-pointer, which points to r1. "this" is defined as "a pointer that points to the object for which the member function is called". I hope that helps, just ask if I didn't explain it good enough.
-
Dear all, I am having a problem in understanding operator overloading in C++. Below is the code I am reading and its explanation. There are some points that I do not really understand. [CODE] class Ratio { public: Ratio(int =0, int =1); //default constructor Ration(const Ratio&); //copy constructor Ration& operator=(const Ratio&); //assignment operator private: int num, den; }; Ratio& Ratio::operator=(const Ratio& r) { num = r.num; den = r.den; return *this; } [/CODE] In the book it is written: [QUOTE] The assignment operator is a function that should return the value it assigns.(This is okay) The assignment operator should return a reference to the same type as the object being assigned. The return type is a reference to an object of the same class. ( I would say okay for both statements) But then this means that the function should return the object that is being assigned in order for the assignment chain to work. So when the assignment operator is being overloaded as a class member function, it should return the object that owns the call. Since there is no other name available for this owner object, C++ defines a special pointer, named this pointer, which points to the owner object. [/QUOTE] I do not really ubderstand the rest of the statements about the object that owns the call. Also I do not understand the implementation code of the operator= function. Why do we use the r object to access the member data and then assign them to the same member data? I do not understand the logic behind that piece of code. Regards, grscot
in fact, the
this
pointer is needed because we can write such code :Ratio r1, r2, r3;
//...
r1 = r2 = r3 = Ratio();
to do such cascading calls, the following operator=() must have a state of the precedent object... Moreover, we need the r object because the reference anc the call owner are not necessary the sam, and as the member datas are
private
, you cannot acces them directly. however, a constructor have got permissions that other member function have, so it can read the data from r.den (for example) when another can't do so.
TOXCCT >>> GEII power
-
Dear all, I am having a problem in understanding operator overloading in C++. Below is the code I am reading and its explanation. There are some points that I do not really understand. [CODE] class Ratio { public: Ratio(int =0, int =1); //default constructor Ration(const Ratio&); //copy constructor Ration& operator=(const Ratio&); //assignment operator private: int num, den; }; Ratio& Ratio::operator=(const Ratio& r) { num = r.num; den = r.den; return *this; } [/CODE] In the book it is written: [QUOTE] The assignment operator is a function that should return the value it assigns.(This is okay) The assignment operator should return a reference to the same type as the object being assigned. The return type is a reference to an object of the same class. ( I would say okay for both statements) But then this means that the function should return the object that is being assigned in order for the assignment chain to work. So when the assignment operator is being overloaded as a class member function, it should return the object that owns the call. Since there is no other name available for this owner object, C++ defines a special pointer, named this pointer, which points to the owner object. [/QUOTE] I do not really ubderstand the rest of the statements about the object that owns the call. Also I do not understand the implementation code of the operator= function. Why do we use the r object to access the member data and then assign them to the same member data? I do not understand the logic behind that piece of code. Regards, grscot
class Ratio
{
public:
Ratio(int numerator=0, int denominator=1); //default constructor
Ratio(const Ratio&); //copy constructor
Ratio& operator=(const Ratio&); //assignment operator
private:
int num, den;
};Ratio& Ratio::operator=(const Ratio& r)
{
num = r.num;
den = r.den;
return *this;
}When you refer to data members of a class (
num
andden
in your example) within a member function of the class, it is done implicitly through athis
pointer. The assignment operator member function could have been written as:Ratio& Ratio::operator=(const Ratio& r)
{
this->num = r.num;
this->den = r.den;
return *this;
}Suppose you had the following example:
Ratio r1(1,2);
Ratio r2(3,4);
r2 = r1;After the assignment, you would expect
r2
to contain 3/4. The compiler implements the call to the assignment operator as if you had written:r2.operator=(r1);
In this case, the assignment code that executes is:
(&r2)->num = r1.num;
(&r2)->den = r1.den;The
this
pointer contains the address of variabler2
, and the argument is a reference to variabler1
. Returning a reference tothis
using 'return *this;
' makes assignment chaining work as follows. Suppose our example is this:Ratio r1(1,2), r2(3,4), r3(5,6);
r3 = r2 = r1;After the assignment statement, you expect all three variables to have the same value of 1/2. The compiler implements this statement as if it had been written:
r3.operator=(r2.operator=(r1));
The
r3.operator=
function is called with the return value of ther2.operator=
call, which is a reference tor2
.
Software Zen:
delete this;
-
class Ratio
{
public:
Ratio(int numerator=0, int denominator=1); //default constructor
Ratio(const Ratio&); //copy constructor
Ratio& operator=(const Ratio&); //assignment operator
private:
int num, den;
};Ratio& Ratio::operator=(const Ratio& r)
{
num = r.num;
den = r.den;
return *this;
}When you refer to data members of a class (
num
andden
in your example) within a member function of the class, it is done implicitly through athis
pointer. The assignment operator member function could have been written as:Ratio& Ratio::operator=(const Ratio& r)
{
this->num = r.num;
this->den = r.den;
return *this;
}Suppose you had the following example:
Ratio r1(1,2);
Ratio r2(3,4);
r2 = r1;After the assignment, you would expect
r2
to contain 3/4. The compiler implements the call to the assignment operator as if you had written:r2.operator=(r1);
In this case, the assignment code that executes is:
(&r2)->num = r1.num;
(&r2)->den = r1.den;The
this
pointer contains the address of variabler2
, and the argument is a reference to variabler1
. Returning a reference tothis
using 'return *this;
' makes assignment chaining work as follows. Suppose our example is this:Ratio r1(1,2), r2(3,4), r3(5,6);
r3 = r2 = r1;After the assignment statement, you expect all three variables to have the same value of 1/2. The compiler implements this statement as if it had been written:
r3.operator=(r2.operator=(r1));
The
r3.operator=
function is called with the return value of ther2.operator=
call, which is a reference tor2
.
Software Zen:
delete this;
-
Dear all, I am having a problem in understanding operator overloading in C++. Below is the code I am reading and its explanation. There are some points that I do not really understand. [CODE] class Ratio { public: Ratio(int =0, int =1); //default constructor Ration(const Ratio&); //copy constructor Ration& operator=(const Ratio&); //assignment operator private: int num, den; }; Ratio& Ratio::operator=(const Ratio& r) { num = r.num; den = r.den; return *this; } [/CODE] In the book it is written: [QUOTE] The assignment operator is a function that should return the value it assigns.(This is okay) The assignment operator should return a reference to the same type as the object being assigned. The return type is a reference to an object of the same class. ( I would say okay for both statements) But then this means that the function should return the object that is being assigned in order for the assignment chain to work. So when the assignment operator is being overloaded as a class member function, it should return the object that owns the call. Since there is no other name available for this owner object, C++ defines a special pointer, named this pointer, which points to the owner object. [/QUOTE] I do not really ubderstand the rest of the statements about the object that owns the call. Also I do not understand the implementation code of the operator= function. Why do we use the r object to access the member data and then assign them to the same member data? I do not understand the logic behind that piece of code. Regards, grscot
To understand the concept of = operator, take the scenario, i = j = k; In this k should be assigned to j and then j should be assigned to i. This is the reason, why we make the return type of the =operator as reference and not by value. If you are passing by value, u will have problem, if u have a pointer data member and u are allocating new memory and don't have a copy constructor. By using return by reference you are actually saving on time as well as memory. Rahul