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. C++ operator

C++ operator

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionc++
8 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.
  • F Offline
    F Offline
    Fred M
    wrote on last edited by
    #1

    Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.

    E A J M 4 Replies Last reply
    0
    • F Fred M

      Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.

      E Offline
      E Offline
      Ernest Laurentin
      wrote on last edited by
      #2

      In that case you must declare the operator in class bb the same way than in class aa. Did you notice that they are not compatible? They both must be virtual. (In fact, that's the first time I saw someone using virtual operator but hey, it's good to know. Thanks).

      1 Reply Last reply
      0
      • F Fred M

        Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.

        A Offline
        A Offline
        Alvaro Mendez
        wrote on last edited by
        #3

        Since the bb operator is not declared exactly as the aa operator, they're not treated polymorphically. In other words, they appear as two different functions and the compiler uses the one that most closely matches. Regards, Alvaro

        F 1 Reply Last reply
        0
        • A Alvaro Mendez

          Since the bb operator is not declared exactly as the aa operator, they're not treated polymorphically. In other words, they appear as two different functions and the compiler uses the one that most closely matches. Regards, Alvaro

          F Offline
          F Offline
          Fred M
          wrote on last edited by
          #4

          Ok , thank's a lot :) But do you know the solution for my problem.

          A 1 Reply Last reply
          0
          • F Fred M

            Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.

            J Offline
            J Offline
            Jonathan Gilligan
            wrote on last edited by
            #5

            * reinterpret_cast<bb *>(xx) = * reinterpret_cast<bb *>(yy); Bomb our homes and threaten our children, and, as difficult as it is, we will still love you --- Martin Luther King, Jr.

            F 1 Reply Last reply
            0
            • J Jonathan Gilligan

              * reinterpret_cast<bb *>(xx) = * reinterpret_cast<bb *>(yy); Bomb our homes and threaten our children, and, as difficult as it is, we will still love you --- Martin Luther King, Jr.

              F Offline
              F Offline
              Fred M
              wrote on last edited by
              #6

              Thank's, but sometimes i don't know the real type of xx and yy, and i dont want to know. I don't write this code with a switch. Other idea ?

              1 Reply Last reply
              0
              • F Fred M

                Ok , thank's a lot :) But do you know the solution for my problem.

                A Offline
                A Offline
                Alvaro Mendez
                wrote on last edited by
                #7

                The solution is to add another assignment operator inside bb that looks exactly like the one in aa. Then you can call bb operator from there by casting the parameter to bb&:

                virtual aa& operator=(const aa& c) throw()
                {
                return operator=((bb&)c);
                }

                Regards, Alvaro

                1 Reply Last reply
                0
                • F Fred M

                  Hi, everybody, i need some help for writing an operator= ::::::::::::::::::::::::::::::: class aa { public: int var; virtual aa& operator=(const aa && c ) throw(); }; ::::::::::::::::::::::::::::::: class bb : public aa { public: int varbb; bb& operator=(const bb && c ) { varbb=c.varbb; return *this; }; }; int main() { aa * xx = new bb(); aa * yy = new bb(); *xx=*yy; }; Problem: Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=. How can i do that? Ho, sorry for my english.

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #8

                  Can operators even be virtual? You certainly can't make = virtual, because = always operates on a object, not a pointer to an object, as is required for polymorphism. --Mike-- http://home.inreach.com/mdunn/ Help! Help! I'm being repressed!! :love: your :bob: with :vegemite: and :beer: Sonork - 100.10414 AcidHelm

                  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