Results 1 to 4 of 4

Thread: C++ Operator Overloading - Reference?

  1. #1
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default C++ Operator Overloading - Reference?

    In the following C++ class:

    Code:
    class Counter
    {
    public:
        Counter();
        ~Counter();
        int GetItsVal(void) const { return itsVal; }
        void SetItsVal(int x) { itsVal = x; }
        void Increment(void) { ++itsVal; }
        const Counter& operator++ ();
    
    private:
        int itsVal;
    };
    
    Counter::Counter():
    itsVal(0)
    {}
    
    const Counter& Counter::operator++()
    {
        ++itsVal;
        return *this;
    }
    Why is the operator++ overloader a reference and not regular w/out the & (ampersand)?

    Explaination of how operator overloading works would also be acceptable. Thanks in advance.

    -magicyte

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's a reference because if it weren't the object would be passed by value, which would involve creating a whole new instance of a potentially huge object.

    Operator overloading simply allows you to define the behaviour of existing built-in operators on your class. It's implemented in the form of a method <your class>::operator<relevant operator>, and types are pretty much intuitive — e.g.
    Code:
    const T T::operator+(const T &rhs);
    T& T::operator=(const T &rhs);
    T& T::operator!();
    bool T::operator==(const T &rhs);
    There are (surprisingly for C++) only a couple of ugly hacks and weird gotchas to look out for:
    Code:
    T& T::operator++(); // this is prefix (++foo)
    T& T::operator++(int unusedIntParameter); // this is postfix (foo++)
    (applies to -- as well).
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    It's a reference because if it weren't the object would be passed by value, which would involve creating a whole new instance of a potentially huge object.
    Oh! I remember now! In my C++ book, it talked about making a reference for something so that a whole new object wasn't to created (in other words, a reference to the current object). Didn't get it, but after [sarcasm]HOURS //more like minutes![/sarcasm] of thinking, it registered (though it was tough on my mind ).

    Just been trying to learn about operator overloading, and finally figured more that half of it out, thanks to you.

    Why does the overloaded '++' function in Counter return the this pointer? And what is the this pointer's value in this case?

    -magicyte

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    this is what it always is: a pointer to the object on which the method is being invoked. ++ returns a reference (note that the pointer is dereferenced) because that's what the operator does: increments its operand and then returns the incremented operand.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •