Thread: What does this code mean exactly?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    9

    What does this code mean exactly?

    My book doesn't have the best explanation in the world and I would appreciate it if someone would break this down with as much detail as they can stand, but to give the lazy ones some general guide lines ... what I'm mainly concerned w/ here are the references and operator overloading, but as a new programmer I could benefit from any extra explanations you'd want to add.


    #include <iostream>

    using namespace std;

    class Counter
    {
    public:
    Counter();
    ~Counter(){}
    int GetItsVal() const {return itsVal; }
    void SetItsVal(int x) {ivsVal = x; }
    const Counter& operator++ (); //prefix
    const Counter operator++ (int); //postfix

    private:
    int itsVal;
    };

    Counter::Counter:
    itsVal(0)
    {}

    const Counter& Counter:perator++()
    {
    ++itsVal;
    return *this;
    }

    const Counter Counter:perator++(int theFlag)
    {
    Counter temp(*this);
    ++itsVal;
    return temp;
    }

    int main()
    {
    Counter i;
    cout << "The value of i is " << i.GetItsVal() << endl;
    i++;
    cout << The Value of i is " << i.GetItsVal() << endl;
    ++i;
    cout << The Value of i is " << i.GetItsVal() << endl;
    Counter a = ++i;
    cout << "The value of a: " << a.GetItsVal();
    cout << " and i: " << i.GetItsVal() ,, endl:
    a = i++
    cout << "The value of a: " << a.GetItsVal();
    cout << " and i: " << i.GetItsVal() << endl;
    return 0;
    }

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    there's a lot of explanation here.. it all depends of the level of your knowledge....


    hint: when you "overload" an operator inside the body of your class... it means that the normal behavior of such operator will be altered to your specifications when 2 objects of ONLY that class, where the operator is overloaded , are joined....

    for example.....

    if a = 1 and b = 2

    a + b = 3

    however if you overload the operator like this (alter)

    class p
    {
    p operator +(&p){ p.rhs - some.p}
    };

    lets say you declare p a;
    and p b;

    and you do this.....

    p.a + p.b = -1;

    b/c in this case both (a) and (b) are objects of that class thus + operator is altered to acutally subtract the passed in values.......

    hope that didn't confuse you...

    Regards,
    matheo917

    ps. there's a lot of detail here....

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    That actually cleared things up nicely, i wasn't sure if the word operator was something that was used in the book as a variable name that was supposed to be clear or if the actual word operator was necessary. One thing I'm still a little unclear about why a reference is necessary.

    class p
    {
    p operator +(&p){ p.rhs - some.p}
    };

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    12
    the reference isn't necessary, but if you leave it out, that means creating a copy of the object, which could be very expensive for large objects.. and since we're not modifying the object at all, it's perfectly safe to pass by reference, and you would usually declare the fact that you're not going to modify it using the keyword 'const'

    Code:
    class Blah {
      int blah;
    
    public:
      bool operator == (const Blah &rhs)
      {
        return (blah == rhs.blah);
      }
    };
    const Blah &rhs //rhs is a const reference, meaning we aren't going to modify rhs..

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    I would just like to apologize for just storming onto your board and asking a question w/o searching. I always rag on people on other boards for not searching questions that are answered 50 times. You've got a good board here, I've searched and cleared up most of my confusion in an hour. Once again, i apologize for the rererererererepost ... but since it's already here, I may as well keep going . I get that references are used to change variables and keep the changes accessable outside of a function, but I feel like there's another level of understanding to it that I'm just not getting. I know this is an extremely vague question, but maybe you've had a previous experience where you felt you were missing something as far as understanding references ... I'm probably missing that to so just relate that little bit of info if ya don't mind. Geez this is a long post ... and I'll use [ code ] [ / code ] next time .

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    12
    References are just pointers, so maybe the mis-understanding has to do with pointers? I'd explain them, but you'd prolly be better off learning pointers from a book or other online source

    quick google search gives some pointer tutorials:
    http://www.intap.net/~drw/cpp/cpp08_01.htm
    http://carbon.cudenver.edu/~tgibson/tutorial/ptr.html

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    9
    I just came to realize I don't know why I'm doing this ... I went back and looked in my book, it's likely I overlooked it, but what I've managed to gleen is that it's for clarity?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    Originally posted by matheo917
    it means that the normal behavior of such operator will be altered to your specifications when 2 objects of ONLY that class, where the operator is overloaded , are joined....
    actually you can overload an operator for almost any situation, it just takes careful planning.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM