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. overloaded = operator not being invoked

overloaded = operator not being invoked

Scheduled Pinned Locked Moved C / C++ / MFC
debugging
7 Posts 2 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.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    Hi my overloaded operator are in my header so I make a breakpoint watching them being invoked except for the = operator hope some can tell me why i'll post first type of struct using range

    ;
    struct usingrange
    {
    ESDID esdid;
    int start;
    int end;
    uint32_t operator=(const BYTE z[4])
    {
    return ((z[0] << 24) | (z[1] << 16) | (z[2] << 8) | z[3]);

    };
    

    bool operator<(const usingrange x) const {
    if (esdid < x.esdid) return esdid < x.esdid;
    if (start < x.start) return start < x.start; if (end < x.end) return end < x.end;
    };

    };

    next my code with the = operator the lvalue is member of using range I would think that would casue invocation of my overloaded operator

    	usingrange using\_range{ usingpoint->usingesdid };
    	
    	using\_range.start = (BYTE &)usingpoint->statement;
        using\_range.end = (BYTE &)usingpoint->laststatement;
    
    Mircea NeacsuM 1 Reply Last reply
    0
    • F ForNow

      Hi my overloaded operator are in my header so I make a breakpoint watching them being invoked except for the = operator hope some can tell me why i'll post first type of struct using range

      ;
      struct usingrange
      {
      ESDID esdid;
      int start;
      int end;
      uint32_t operator=(const BYTE z[4])
      {
      return ((z[0] << 24) | (z[1] << 16) | (z[2] << 8) | z[3]);

      };
      

      bool operator<(const usingrange x) const {
      if (esdid < x.esdid) return esdid < x.esdid;
      if (start < x.start) return start < x.start; if (end < x.end) return end < x.end;
      };

      };

      next my code with the = operator the lvalue is member of using range I would think that would casue invocation of my overloaded operator

      	usingrange using\_range{ usingpoint->usingesdid };
      	
      	using\_range.start = (BYTE &)usingpoint->statement;
          using\_range.end = (BYTE &)usingpoint->laststatement;
      
      Mircea NeacsuM Offline
      Mircea NeacsuM Offline
      Mircea Neacsu
      wrote on last edited by
      #2

      Again, not sure which one of the 3 statements you would expect to invoke the assignment operator. The first one:

      usingrange using_range{ usingpoint->usingesdid };

      is a declaration that invokes a constructor. As you don't have a constructor, the compiler creates a default one. The second and the third:

      using_range.start = (BYTE &)usingpoint->statement;
      using_range.end = (BYTE &)usingpoint->laststatement;

      are simply integer assignments. If you would want to invoke the assignment operator you would need to write something like

      usingrange using_range2;
      using_range2 = using_range;

      Mircea

      F 1 Reply Last reply
      0
      • Mircea NeacsuM Mircea Neacsu

        Again, not sure which one of the 3 statements you would expect to invoke the assignment operator. The first one:

        usingrange using_range{ usingpoint->usingesdid };

        is a declaration that invokes a constructor. As you don't have a constructor, the compiler creates a default one. The second and the third:

        using_range.start = (BYTE &)usingpoint->statement;
        using_range.end = (BYTE &)usingpoint->laststatement;

        are simply integer assignments. If you would want to invoke the assignment operator you would need to write something like

        usingrange using_range2;
        using_range2 = using_range;

        Mircea

        F Offline
        F Offline
        ForNow
        wrote on last edited by
        #3

        these two the lvalue is a int and a member of using range the rvalue is of type BYTE [4] ? I am sure I am not understanding something ?

        using_range.start = *usingpoint->statement;
        using_range.end = *usingpoint->laststatement;

        Mircea NeacsuM 1 Reply Last reply
        0
        • F ForNow

          these two the lvalue is a int and a member of using range the rvalue is of type BYTE [4] ? I am sure I am not understanding something ?

          using_range.start = *usingpoint->statement;
          using_range.end = *usingpoint->laststatement;

          Mircea NeacsuM Offline
          Mircea NeacsuM Offline
          Mircea Neacsu
          wrote on last edited by
          #4

          If statement is BYTE[4], usingpoint->statement is a pointer to a BYTE value. So *usingpoint->laststatement is the content of the first byte. It can be assigned to an integer without any problem. That's probably not what you want but that's what the compiler understood. The assignment operator that you defined is for assigning the WHOLE structure. It doesn't apply to individual members.

          Mircea

          F 1 Reply Last reply
          0
          • Mircea NeacsuM Mircea Neacsu

            If statement is BYTE[4], usingpoint->statement is a pointer to a BYTE value. So *usingpoint->laststatement is the content of the first byte. It can be assigned to an integer without any problem. That's probably not what you want but that's what the compiler understood. The assignment operator that you defined is for assigning the WHOLE structure. It doesn't apply to individual members.

            Mircea

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            you are right I saw that in that disassembly however I made a breakpoint at "int operator+..." and it never got invoked probably because as you said the rvalue is BYTE while in the operator= parameter it BYTE z[4]; when I have the code

            using_range.start = usingpoint->statement;

            I got an error message from intellsense saying cannt assign int to BYTE *

            Mircea NeacsuM 1 Reply Last reply
            0
            • F ForNow

              you are right I saw that in that disassembly however I made a breakpoint at "int operator+..." and it never got invoked probably because as you said the rvalue is BYTE while in the operator= parameter it BYTE z[4]; when I have the code

              using_range.start = usingpoint->statement;

              I got an error message from intellsense saying cannt assign int to BYTE *

              Mircea NeacsuM Offline
              Mircea NeacsuM Offline
              Mircea Neacsu
              wrote on last edited by
              #6

              I think you misunderstood the functioning of overloaded operators. I'll try to explain but it's best if you check with a proper manual. I'm not a particularly good teacher :) If you have something like:

              struct A {
              char ch;
              long l;
              //...
              };

              struct B {
              int i;
              char c;
              //...
              };

              A a;
              B b;

              If you write an assignment:

              a = b;

              The compiler will complain that it doesn't know how to assign a B to an A. Kind of an apples and oranges problem. However you can write:

              a.l = b.i;
              a.ch = b.c;

              because those are standard types and compiler knows how to perform those assignments without needing a user defined assignment operator. Now we add a user defined assignment operator:

              struct A {
              char ch;
              long l;
              //...
              int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
              };

              After that we can write:

              a = b;

              and the compiler will know how to do the assignment. You can have many such assignment operators. For instance you might add another one to assign an A to another A:

              struct A {
              char ch;
              long l;
              //...
              int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
              int operator =(A& rhs) {l = rhs.l; ch = rhs.ch; return 1}
              };

              The assignment operator that takes an object of the same kind on the right hand side is called "principal assignment operator". Note however that we cannot "chain" the assignment operator. If we write:

              A a1;
              a1 = a = b;

              we will get an error because the result of

              a = b

              is an int and cannot be assigned to an A. To respect the conventions we would have to change the signature of the principal assignment operator:

              struct A {
              char ch;
              long l;
              //...
              int operator =(const B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
              A& operator =(const A& rhs) {l = rhs.l; ch = rhs.ch; return *this;}
              };

              Note that I also made the right hand side a reference to a const. I hope you will find useful my little lecture on assignment operators but, again, best would be to check with a real manual.

              Mircea

              F 1 Reply Last reply
              0
              • Mircea NeacsuM Mircea Neacsu

                I think you misunderstood the functioning of overloaded operators. I'll try to explain but it's best if you check with a proper manual. I'm not a particularly good teacher :) If you have something like:

                struct A {
                char ch;
                long l;
                //...
                };

                struct B {
                int i;
                char c;
                //...
                };

                A a;
                B b;

                If you write an assignment:

                a = b;

                The compiler will complain that it doesn't know how to assign a B to an A. Kind of an apples and oranges problem. However you can write:

                a.l = b.i;
                a.ch = b.c;

                because those are standard types and compiler knows how to perform those assignments without needing a user defined assignment operator. Now we add a user defined assignment operator:

                struct A {
                char ch;
                long l;
                //...
                int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
                };

                After that we can write:

                a = b;

                and the compiler will know how to do the assignment. You can have many such assignment operators. For instance you might add another one to assign an A to another A:

                struct A {
                char ch;
                long l;
                //...
                int operator =(B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
                int operator =(A& rhs) {l = rhs.l; ch = rhs.ch; return 1}
                };

                The assignment operator that takes an object of the same kind on the right hand side is called "principal assignment operator". Note however that we cannot "chain" the assignment operator. If we write:

                A a1;
                a1 = a = b;

                we will get an error because the result of

                a = b

                is an int and cannot be assigned to an A. To respect the conventions we would have to change the signature of the principal assignment operator:

                struct A {
                char ch;
                long l;
                //...
                int operator =(const B& rhs) {l = rhs.i; ch = rhs.c; return 1;}
                A& operator =(const A& rhs) {l = rhs.l; ch = rhs.ch; return *this;}
                };

                Note that I also made the right hand side a reference to a const. I hope you will find useful my little lecture on assignment operators but, again, best would be to check with a real manual.

                Mircea

                F Offline
                F Offline
                ForNow
                wrote on last edited by
                #7

                thanks beginning to see there is not a way to do what I want with operator overloading

                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