Thread: member function syntax

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    14

    Unhappy member function syntax

    I'm working thru SAMS C++ in 21days and am confused by the operator overloading. Here's some code:

    #include <iostream>
    #include <stdlib.h>

    class Counter
    {
    public:

    Counter(int val);
    ~Counter(){}
    int GetItsVal()const{return itsVal;}
    void SetItsVal(int x){itsVal=x;}
    void Increment(){++itsVal;}
    Counter operator++();
    private:
    int itsVal;
    };



    Counter::Counter(int val):itsVal(val){}

    Counter Counter:perator++()//WHY IS THIS COUNTER COUNTER? WHY 2 COUNTERS?
    {
    ++itsVal;
    return Counter(itsVal);
    }

    int main()
    {
    Counter i(0);
    cout<<"i: "<<i.GetItsVal()<<endl;
    i.Increment();
    cout<<"i: "<<i.GetItsVal()<<endl;
    ++i;
    cout<<"i: "<<i.GetItsVal()<<endl;
    Counter a=++i;
    cout<<"a: "<<a.GetItsVal()
    <<" i: "<<i.GetItsVal()<<endl;

    system("PAUSE");
    return 0;
    }

    My question in comments above. To elaborate: Is this only done with the operator overloading? Is it because the function returns type Counter?

    Thanks for any help on this...

    BTW. The smiley is not original to the code, but is interpreted by the forums its the second colon and a lower case 'o'
    -Dev-C++ 5.beta somesuch
    --"Hello world!" (52%)

    "Stupidity - the number one sexually transmitted disease."

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    The first Counter is the return type, the second makes clear that it is a member function of that class.

  3. #3
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    If you're referring to the syntax, then all that this
    Code:
    Counter Counter::operator++()
    Counter:perator++() <<--- This is the member function

    The 'Counter' before that is the type that is returned.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    14

    Smile

    thanks guys. I was hoping it was that simple.
    -Dev-C++ 5.beta somesuch
    --"Hello world!" (52%)

    "Stupidity - the number one sexually transmitted disease."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM