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. Operator overloading

Operator overloading

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
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.
  • T Offline
    T Offline
    tbrake
    wrote on last edited by
    #1

    Hallo I have written some class #if !defined(_COMPLEX_H_) #define _COMPLEX_H_ #include <stdio.h> #include <iostream.h> class Complex { public: Complex(); Complex(const Complex&); Complex(double a, double b) : re(a), im(b) {} virtual ~Complex(); // Complex += Complex const Complex& operator+= (const Complex&); // Cpmplex += Zahl const Complex& operator+= (const double); // Zahl += Complex friend Complex operator+= (double&, Complex&); // Comlex + Complex Complex operator+ (const Complex&) const; // Complex + Zahl Complex operator+ (const double) const; // Zahl + Complex friend Complex operator+ (double, const Complex&); // Complex -= Complex const Complex& operator-= (const Complex&); // Complex -= Zahl const Complex& operator-= (const double); // Zahl -= Complex friend Complex operator-= (double, Complex&); // Complex - Complex Complex operator- (const Complex&) const; // Complex - Zahl Complex operator- (const double) const; // Zahl - Complex friend Complex operator- (double, const Complex&); // Konjunktion Complex operator! (void); // Ausgabe einer Complexenzahl friend ostream& operator<< (ostream&,const Complex&); private: double re; double im; }; #endif How to change this operator to global calling and then referencing in the class complex ??? THX T

    R B T 3 Replies Last reply
    0
    • T tbrake

      Hallo I have written some class #if !defined(_COMPLEX_H_) #define _COMPLEX_H_ #include <stdio.h> #include <iostream.h> class Complex { public: Complex(); Complex(const Complex&); Complex(double a, double b) : re(a), im(b) {} virtual ~Complex(); // Complex += Complex const Complex& operator+= (const Complex&); // Cpmplex += Zahl const Complex& operator+= (const double); // Zahl += Complex friend Complex operator+= (double&, Complex&); // Comlex + Complex Complex operator+ (const Complex&) const; // Complex + Zahl Complex operator+ (const double) const; // Zahl + Complex friend Complex operator+ (double, const Complex&); // Complex -= Complex const Complex& operator-= (const Complex&); // Complex -= Zahl const Complex& operator-= (const double); // Zahl -= Complex friend Complex operator-= (double, Complex&); // Complex - Complex Complex operator- (const Complex&) const; // Complex - Zahl Complex operator- (const double) const; // Zahl - Complex friend Complex operator- (double, const Complex&); // Konjunktion Complex operator! (void); // Ausgabe einer Complexenzahl friend ostream& operator<< (ostream&,const Complex&); private: double re; double im; }; #endif How to change this operator to global calling and then referencing in the class complex ??? THX T

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      tbrake wrote:

      global calling

      What do you mean ? What do you want to achieve ? ~RaGE();

      1 Reply Last reply
      0
      • T tbrake

        Hallo I have written some class #if !defined(_COMPLEX_H_) #define _COMPLEX_H_ #include <stdio.h> #include <iostream.h> class Complex { public: Complex(); Complex(const Complex&); Complex(double a, double b) : re(a), im(b) {} virtual ~Complex(); // Complex += Complex const Complex& operator+= (const Complex&); // Cpmplex += Zahl const Complex& operator+= (const double); // Zahl += Complex friend Complex operator+= (double&, Complex&); // Comlex + Complex Complex operator+ (const Complex&) const; // Complex + Zahl Complex operator+ (const double) const; // Zahl + Complex friend Complex operator+ (double, const Complex&); // Complex -= Complex const Complex& operator-= (const Complex&); // Complex -= Zahl const Complex& operator-= (const double); // Zahl -= Complex friend Complex operator-= (double, Complex&); // Complex - Complex Complex operator- (const Complex&) const; // Complex - Zahl Complex operator- (const double) const; // Zahl - Complex friend Complex operator- (double, const Complex&); // Konjunktion Complex operator! (void); // Ausgabe einer Complexenzahl friend ostream& operator<< (ostream&,const Complex&); private: double re; double im; }; #endif How to change this operator to global calling and then referencing in the class complex ??? THX T

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Hello, Does the following help:

        Complex operator!(const Complex& This);

        This operator is not a member function, but a global one. Behind every great black man...             ... is the police. - Conspiracy brother Blog[^]

        1 Reply Last reply
        0
        • T tbrake

          Hallo I have written some class #if !defined(_COMPLEX_H_) #define _COMPLEX_H_ #include <stdio.h> #include <iostream.h> class Complex { public: Complex(); Complex(const Complex&); Complex(double a, double b) : re(a), im(b) {} virtual ~Complex(); // Complex += Complex const Complex& operator+= (const Complex&); // Cpmplex += Zahl const Complex& operator+= (const double); // Zahl += Complex friend Complex operator+= (double&, Complex&); // Comlex + Complex Complex operator+ (const Complex&) const; // Complex + Zahl Complex operator+ (const double) const; // Zahl + Complex friend Complex operator+ (double, const Complex&); // Complex -= Complex const Complex& operator-= (const Complex&); // Complex -= Zahl const Complex& operator-= (const double); // Zahl -= Complex friend Complex operator-= (double, Complex&); // Complex - Complex Complex operator- (const Complex&) const; // Complex - Zahl Complex operator- (const double) const; // Zahl - Complex friend Complex operator- (double, const Complex&); // Konjunktion Complex operator! (void); // Ausgabe einer Complexenzahl friend ostream& operator<< (ostream&,const Complex&); private: double re; double im; }; #endif How to change this operator to global calling and then referencing in the class complex ??? THX T

          T Offline
          T Offline
          tbrake
          wrote on last edited by
          #4

          Hi i'd like to achive that there are not that many functions so changes might be easier to make I know that it is possible to get the same behavior by using 4 global funktions and torefere by using friend. But i dont know hoe to manage T

          D 1 Reply Last reply
          0
          • T tbrake

            Hi i'd like to achive that there are not that many functions so changes might be easier to make I know that it is possible to get the same behavior by using 4 global funktions and torefere by using friend. But i dont know hoe to manage T

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            tbrake wrote:

            I know that it is possible to get the same behavior by using 4 global funktions...

            In addition to, or in lieu of, the class methods?

            tbrake wrote:

            ...and torefere by using friend.

            Doesn't your class already have friend functions?


            "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

            T 1 Reply Last reply
            0
            • D David Crow

              tbrake wrote:

              I know that it is possible to get the same behavior by using 4 global funktions...

              In addition to, or in lieu of, the class methods?

              tbrake wrote:

              ...and torefere by using friend.

              Doesn't your class already have friend functions?


              "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

              T Offline
              T Offline
              tbrake
              wrote on last edited by
              #6

              Year your right !?! But whenever i try something like Complex operator! (double&, Complex&); class Complex { ... }; I receive some errors! And second isn't ist possible to combine all "+" or "+=" ... functions so that there is only one to modify and not like the class i posted changeing always 3 methods ?? T

              D 1 Reply Last reply
              0
              • T tbrake

                Year your right !?! But whenever i try something like Complex operator! (double&, Complex&); class Complex { ... }; I receive some errors! And second isn't ist possible to combine all "+" or "+=" ... functions so that there is only one to modify and not like the class i posted changeing always 3 methods ?? T

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                tbrake wrote:

                I receive some errors!

                What errors?

                class Complex
                {
                ...
                };

                Complex operator!( double&, Complex& );


                "The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli

                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