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. Equality ( operator== ) Overload

Equality ( operator== ) Overload

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomregextutorial
7 Posts 4 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.
  • J Offline
    J Offline
    Jeryth
    wrote on last edited by
    #1

    I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this: bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases } Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth

    Y S K J 4 Replies Last reply
    0
    • J Jeryth

      I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this: bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases } Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth

      Y Offline
      Y Offline
      YaronNir
      wrote on last edited by
      #2

      Here is a way to do it

      Class YourClass
      {
      public:
      void operator=(const &YourClass c);

      private:
      int iNum;
      CString szStr;
      }

      void YourClass::operator=(const &YourClass c)
      {
      iNum = c.iNum;
      szStr = c.szStr;
      }

      Ask not what your application can do for you, Ask what you can do for your application

      S 1 Reply Last reply
      0
      • Y YaronNir

        Here is a way to do it

        Class YourClass
        {
        public:
        void operator=(const &YourClass c);

        private:
        int iNum;
        CString szStr;
        }

        void YourClass::operator=(const &YourClass c)
        {
        iNum = c.iNum;
        szStr = c.szStr;
        }

        Ask not what your application can do for you, Ask what you can do for your application

        S Offline
        S Offline
        Steen Krogsgaard 2
        wrote on last edited by
        #3

        Excellent example of an assignment operator overload. Unfortunately this was not what the OP wanted, he asked for a equality operator overload! :rolleyes:

        Y 1 Reply Last reply
        0
        • J Jeryth

          I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this: bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases } Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth

          S Offline
          S Offline
          Steen Krogsgaard 2
          wrote on last edited by
          #4

          bool CLASS_NAME::operator==(CLASS_NAME rhs) const may also work, but I don't think there's anything wrong with your version. There's a nice explanation of operator overloading at http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.5[^] Cheers Steen

          1 Reply Last reply
          0
          • J Jeryth

            I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this: bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases } Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth

            K Offline
            K Offline
            karle
            wrote on last edited by
            #5

            Your operator is correct !

            1 Reply Last reply
            0
            • S Steen Krogsgaard 2

              Excellent example of an assignment operator overload. Unfortunately this was not what the OP wanted, he asked for a equality operator overload! :rolleyes:

              Y Offline
              Y Offline
              YaronNir
              wrote on last edited by
              #6

              opps :) about equality,your code looks ok...so there shouldn't be any problem Ask not what your application can do for you, Ask what you can do for your application

              1 Reply Last reply
              0
              • J Jeryth

                I've never overloaded the equality operator and am having difficulty finding example code in MSDN for doing so. I'm guessing it looks a little something like this: bool CLASS_NAME::operator==( const CLASS_NAME & rhs ) const { //Perform tests on length, size //and any other info needed //return true if all match //false for all other cases } Is this correct or am I missing something? ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth

                J Offline
                J Offline
                Jeryth
                wrote on last edited by
                #7

                Wow, got it right on the first guess. Thanks for the confirmation. ________________________________________________________________________ The question "Do computers think?" is the same as "Can submarines swim?" Signature Red Studios Jeryth

                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