#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0

    Overloading << and >> operators (syntax)


    How do I go about coding that in the .h and .cpp files? Currently I have it all in the .h file, but this is causing problems (although the overloading works I think - doesn't give any compile errors) with other parts of the program. I was wondering how the syntax would go as far as declaring the function declaration in the .h file, but putting the actual code in the .cpp?
    Last edited by homncruse; May 15th, 2003 at 11:17 PM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    Blacksburg VA/Philly PA
    Posts
    38
    Rep Power
    13

    Re: Overloading << and >> operators (syntax)


    Originally posted by homncruse
    How do I go about coding that in the .h and .cpp files? Currently I have it all in the .h file, but this is causing problems (although the overloading works I think - doesn't give any compile errors) with other parts of the program. I was wondering how the syntax would go as far as declaring the function declaration in the .h file, but putting the actual code in the .cpp?
    this is pretty easily done, but the overloaded << and >> cannot be members of the class, they have to be declared friends of it. So anywhere outside of the class just declare

    friend ostream& operator(ostream&, cont Object&);


    then in the cpp file just provide the implementation
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    >> but the overloaded << and >> cannot be members of the class, they have to be declared friends of it.
    Depends on what you're doing with the overloaded operators. It is entirely possible for you to declare an overloaded << without declaring it as a friend function. For instance, in the following example, I've overloaded << to add a number and return the resulting int value. BTW homncruse, this answers your question as well :) :
    Code:
    myint.h
    ======
    class MyInt {
    private:
      int val;
    public:
      MyInt(int init);
      int operator <<(int val2);
    };
    
    
    myint.C
    ======
    #include <iostream>
    #include "myint.h"
    using namespace std;
    
    MyInt::MyInt(int init) {
      val = init;
    }
    
    int MyInt::operator<< (int val2) {
      val += val2;
      return val;
    }
    
    int main(void) {
      int x;
      MyInt foo(4);
      x = foo << 2;
    
      cout << x << endl;
      return 0;
    }
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    14
    Rep Power
    0
    Thanks for the help, I've got the overloading working now, but I'm having a problem with pointers :confused:
    To keep subject lines consistent with subjects (so we don't go "off-topic") I'll post that problem in a new thread here

IMN logo majestic logo threadwatch logo seochat tools logo