Using overloaded operators

This is a discussion on Using overloaded operators within the C++ Programming forums, part of the General Programming Boards category; Hi guys Please take a look at this example: Code: class X { public: // member prefix ++x void operator++() ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    140

    Using overloaded operators

    Hi guys

    Please take a look at this example:

    Code:
    class X {
    public:
    
      // member prefix ++x
      void operator++() { }
    };
    
    class Y { };
    
    // non-member prefix ++y
    void operator++(Y&) { }
    
    int main() {
      X x;
      Y y;
    
      // calls x.operator++()
      ++x;
    
      // explicit call, like ++x
      x.operator++();
    
      // calls operator++(y)
      ++y;
    
      // explicit call, like ++y
      operator++(y);
    }
    Please note that when using the overloaded operator for an instance of X, we can call it using

    Code:
    x.operator++()
    In the case with the overloaded operator ++ for an instance of Y (it is overloaded using a non-member function), we can call it using

    Code:
    operator++(y)
    My question is: Is there any reasoning behind the fact that we cannot call the two operators using the same syntax? Or is that just the way it is?

    Best,
    Niles.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by Niels_M
    Is there any reasoning behind the fact that we cannot call the two operators using the same syntax?
    But you can, i.e., ++x and ++y

    Besides that, please take a look at this example:
    Code:
    class X {
    public:
    
      // member function foo
      void foo() { }
    };
    
    class Y { };
    
    // non-member function foo
    void foo(Y&) { }
    
    int main() {
      X x;
      Y y;
    
      x.foo();
    
      foo(y);
    }
    Please note that when using the function foo for an instance of X, we can call it using
    Code:
    x.foo();
    In the case with the function foo for an instance of Y (it is declared as a non-member function), we can call it using
    Code:
    foo(y);
    My question is: Is there any reasoning behind the fact that we cannot call the functions using the same syntax? Or is that just the way it is?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    140
    Quote Originally Posted by laserlight View Post
    But you can, i.e., ++x and ++y
    Ah, yes. Of course.


    Quote Originally Posted by laserlight View Post
    Besides that, please take a look at this example:
    Code:
    class X {
    public:
    
      // member function foo
      void foo() { }
    };
    
    class Y { };
    
    // non-member function foo
    void foo(Y&) { }
    
    int main() {
      X x;
      Y y;
    
      x.foo();
    
      foo(y);
    }
    Please note that when using the function foo for an instance of X, we can call it using
    Code:
    x.foo();
    In the case with the function foo for an instance of Y (it is declared as a non-member function), we can call it using
    Code:
    foo(y);
    I see, very good explanation. Thanks!

    Best,
    Niles.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 04:07 AM
  3. Polymorphism & Overloaded Operators :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 08:40 PM
  4. overloaded operators
    By ivandn in forum C++ Programming
    Replies: 2
    Last Post: 12-20-2001, 02:52 PM
  5. help with overloaded operators
    By doleman19 in forum C++ Programming
    Replies: 23
    Last Post: 10-23-2001, 02:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21