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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. overload operator... a begginner info

overload operator... a begginner info

Scheduled Pinned Locked Moved C / C++ / MFC
comquestion
5 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.
  • R Offline
    R Offline
    Remi Morin
    wrote on last edited by
    #1

    struct a
    {
    double Val1;
    double Val2;
    double Val3;
    }

    how do I overload the operator (+,-,*,/) and (+=,-=,*=,/=,=) I know that I will be something like

    a a::operator + (a paramA1,a paramA2)
    {
    paramA1.Val1 += paramA2.Val1;
    paramA1.Val1 += paramA2.Val1;
    paramA1.Val1 += paramA2.Val1;
    return paramA1;
    }

    but for the X= (+=,-=...) I want it clean... and I was'nt able to find the ABC of overloading operator's the rules and the basis. thanks Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

    T M 2 Replies Last reply
    0
    • R Remi Morin

      struct a
      {
      double Val1;
      double Val2;
      double Val3;
      }

      how do I overload the operator (+,-,*,/) and (+=,-=,*=,/=,=) I know that I will be something like

      a a::operator + (a paramA1,a paramA2)
      {
      paramA1.Val1 += paramA2.Val1;
      paramA1.Val1 += paramA2.Val1;
      paramA1.Val1 += paramA2.Val1;
      return paramA1;
      }

      but for the X= (+=,-=...) I want it clean... and I was'nt able to find the ABC of overloading operator's the rules and the basis. thanks Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

      T Offline
      T Offline
      Tomasz Sowinski
      wrote on last edited by
      #2

      I was'nt able to find the ABC of overloading operator's the rules and the basis. Where did you search? Tomasz Sowinski -- http://www.shooltz.com

      R 1 Reply Last reply
      0
      • T Tomasz Sowinski

        I was'nt able to find the ABC of overloading operator's the rules and the basis. Where did you search? Tomasz Sowinski -- http://www.shooltz.com

        R Offline
        R Offline
        Remi Morin
        wrote on last edited by
        #3

        in msdn... I've found that I can overload many operator, += << etc. and I cannot overload . :: ?: but no example, for a struct, a class, they said that we can define them as global but when I tried to do it with the = operator.. it didn't work (I've uderstand that I need "this->data"). So, what I want it's the philosophy, my overloaded operator work but I don't think it's the way to do that...

        struct AStruct
        {
        double data[100];
        AStruct &AStruct::operator= (AStruct paramA);
        };

        It's a very beginner info I know but "overloading operator" I'm Ok, but do it well... it's may be an other thing Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

        T 1 Reply Last reply
        0
        • R Remi Morin

          in msdn... I've found that I can overload many operator, += << etc. and I cannot overload . :: ?: but no example, for a struct, a class, they said that we can define them as global but when I tried to do it with the = operator.. it didn't work (I've uderstand that I need "this->data"). So, what I want it's the philosophy, my overloaded operator work but I don't think it's the way to do that...

          struct AStruct
          {
          double data[100];
          AStruct &AStruct::operator= (AStruct paramA);
          };

          It's a very beginner info I know but "overloading operator" I'm Ok, but do it well... it's may be an other thing Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

          T Offline
          T Offline
          Tomasz Sowinski
          wrote on last edited by
          #4

          You should read some good C++ book first - 'C++ Language' by Stroustrup or 'Effective C++' by Meyers. You should pass AStruct by const reference. It's much more effective than passing by value, which involves calling a copy constructor. This applies to all operators and 'normal' functions as well. Tomasz Sowinski -- http://www.shooltz.com

          1 Reply Last reply
          0
          • R Remi Morin

            struct a
            {
            double Val1;
            double Val2;
            double Val3;
            }

            how do I overload the operator (+,-,*,/) and (+=,-=,*=,/=,=) I know that I will be something like

            a a::operator + (a paramA1,a paramA2)
            {
            paramA1.Val1 += paramA2.Val1;
            paramA1.Val1 += paramA2.Val1;
            paramA1.Val1 += paramA2.Val1;
            return paramA1;
            }

            but for the X= (+=,-=...) I want it clean... and I was'nt able to find the ABC of overloading operator's the rules and the basis. thanks Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

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

            For operators where the left argument is an "a" object, make the method a member of the class:

            struct a
            {
            double d1, d2, d3;
            a operator+ (const a& rhs) const;
            };

            a a::operator+ ( const a& rhs ) const
            {
            a retval;

            retval.d1 = d1 + rhs.d1; // note "d1" is accessing this->d1
            retval.d2 = d2 + rhs.d2;
            retval.d3 = d3 + rhs.d3;

            return retval;
            }

            For operators where the left argument is not an "a", you need to make the method global:

            a operator+ (const double d, const a& rhs )
            {
            a retval;

            a.d1 = d + rhs.d1;
            a.d2 = d + rhs.d2;
            a.d3 = d + rhs.d3;

            return retval;
            }

            Note that if you had a class, and the global operator+ needed to access protected/private members of "a", then the global func would have to be declared as a friend. --Mike-- http://home.inreach.com/mdunn/ This posting is provided "as was" with no warranties, guarantees, lotteries, or any of those little bags of peanuts you get on planes. You assume all risk for crossing the street without holding mommy's hand. © 2001 Mike's Classy Software. Member FDIC. If rash develops, discontinue use. :love: your :bob: with :vegemite: and :beer:

            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