operator overloading - global, member or both

This is a discussion on operator overloading - global, member or both within the C++ Programming forums, part of the General Programming Boards category; Hey guys, would this be correct(practising old exams) example when an operator overloaded function could be member or global function ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    operator overloading - global, member or both

    Hey guys, would this be correct(practising old exams)

    example when an operator overloaded function could be member or global function
    ++, --,!=

    member function only
    (), ->, []


    global function only
    <<, >>

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    148

    Re: operator overloading - global, member or both

    Originally posted by noob2c

    global function only
    <<, >>
    Hmm,No.
    Code:
    class Foo
    {
    	int val;
    public:
    	ostream& operator<<(ostream& os) {
    		return os << val;
    	}
    	Foo& operator >>(int i) {
    		val >>= i;
    		return *this;
    	}
    };
    
    int main()
    {
    	Foo foo;
    	foo << cout;
    	Foo bar = foo >> 3;
    }

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    then which would fall under that category?

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    I'm not sure there are any global-only operators.

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I always remember being told they were either members functions or friend functions. Members if they modified the class data (e.g +=) and friends if they dont (e.g. <<).
    Couldn't think of anything interesting, cool or funny - sorry.

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    They don't NEED to be friends; they can be global functions that have no privileged access if they don't NEED that privileged access.

    operators are part of the interface of the class, but there is no rules about which ones can/should be members or friends or globals. They should exist in the same namespace as the class, but that's about it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Replies: 2
    Last Post: 04-22-2008, 01:07 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 12:14 PM

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