Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. operator= understanding problem

operator= understanding problem

Scheduled Pinned Locked Moved C / C++ / MFC
c++jsonhelpquestionlearning
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    grscot
    wrote on last edited by
    #1

    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

    P T G R 4 Replies Last reply
    0
    • G 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

      P Offline
      P Offline
      pie
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • G 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

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • G 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

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          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 and den in your example) within a member function of the class, it is done implicitly through a this 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 variable r2, and the argument is a reference to variable r1. Returning a reference to this 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 the r2.operator= call, which is a reference to r2.


          Software Zen: delete this;

          T 1 Reply Last reply
          0
          • G Gary R Wheeler

            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 and den in your example) within a member function of the class, it is done implicitly through a this 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 variable r2, and the argument is a reference to variable r1. Returning a reference to this 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 the r2.operator= call, which is a reference to r2.


            Software Zen: delete this;

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            great :doh:


            TOXCCT >>> GEII power

            1 Reply Last reply
            0
            • G 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

              R Offline
              R Offline
              Rahul200676
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups