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. overloading in C++

overloading in C++

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++visual-studioregex
7 Posts 3 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.
  • O Offline
    O Offline
    OmarSH
    wrote on last edited by
    #1

    hello everybody : anyway i am working on program that use alot of object if i want to compare in (if statement) like

    if(ob.x==ob1.x)
    if(ob.x != ob1.x)

    do i need to overload these operators or not (because visual studio says no operator match , if yes could you tell me the code with simple explain thanks anyway .

    C _ 2 Replies Last reply
    0
    • O OmarSH

      hello everybody : anyway i am working on program that use alot of object if i want to compare in (if statement) like

      if(ob.x==ob1.x)
      if(ob.x != ob1.x)

      do i need to overload these operators or not (because visual studio says no operator match , if yes could you tell me the code with simple explain thanks anyway .

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      if the compiler says there is no operator, then yes, you have to overload == http://courses.cms.caltech.edu/cs11/material/cpp/donnie/cpp-ops.html[^]

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • O OmarSH

        hello everybody : anyway i am working on program that use alot of object if i want to compare in (if statement) like

        if(ob.x==ob1.x)
        if(ob.x != ob1.x)

        do i need to overload these operators or not (because visual studio says no operator match , if yes could you tell me the code with simple explain thanks anyway .

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        Assuming x is an object of type X, you will need to overload the == and != operators in class X.

        class X
        {
        bool operator ==(const X& rhs)
        {
        return this.var == rhs.var ? true : false; // Assuming var is a member variable of fundamental type like int.
        }

        bool operator !=(const X& rhs)
        {
            return this.var == rhs.var ? false : true;   // Assuming var is a member variable of fundamental type like int.
        }
        

        };

        The comparison logic would really depend on your particular business case.

        «_Superman_»  _I love work. It gives me something to do between weekends.

        _Microsoft MVP (Visual C++) (October 2009 - September 2013)

        Polymorphism in C

        O 1 Reply Last reply
        0
        • _ _Superman_

          Assuming x is an object of type X, you will need to overload the == and != operators in class X.

          class X
          {
          bool operator ==(const X& rhs)
          {
          return this.var == rhs.var ? true : false; // Assuming var is a member variable of fundamental type like int.
          }

          bool operator !=(const X& rhs)
          {
              return this.var == rhs.var ? false : true;   // Assuming var is a member variable of fundamental type like int.
          }
          

          };

          The comparison logic would really depend on your particular business case.

          «_Superman_»  _I love work. It gives me something to do between weekends.

          _Microsoft MVP (Visual C++) (October 2009 - September 2013)

          Polymorphism in C

          O Offline
          O Offline
          OmarSH
          wrote on last edited by
          #4

          an if X where object from struct not from class ,will the use of this be true????

          _ 1 Reply Last reply
          0
          • O OmarSH

            an if X where object from struct not from class ,will the use of this be true????

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            struct and class are the same in C++. Only difference is that, by default, class members are private and struct members are public.

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

            Polymorphism in C

            O 1 Reply Last reply
            0
            • _ _Superman_

              struct and class are the same in C++. Only difference is that, by default, class members are private and struct members are public.

              «_Superman_»  _I love work. It gives me something to do between weekends.

              _Microsoft MVP (Visual C++) (October 2009 - September 2013)

              Polymorphism in C

              O Offline
              O Offline
              OmarSH
              wrote on last edited by
              #6

              but there is problem :

              this .x

              x is a member in struct not in class , and the hole struct is a private member of class and the overload i need it for object of struct , not the class, so that compiler tel me that x is not a member of class, ( error class y has no member x,

              _ 1 Reply Last reply
              0
              • O OmarSH

                but there is problem :

                this .x

                x is a member in struct not in class , and the hole struct is a private member of class and the overload i need it for object of struct , not the class, so that compiler tel me that x is not a member of class, ( error class y has no member x,

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                Overload the operators inside the struct.

                «_Superman_»  _I love work. It gives me something to do between weekends.

                _Microsoft MVP (Visual C++) (October 2009 - September 2013)

                Polymorphism in C

                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