prefix/postfix ++ & -- operators

This is a discussion on prefix/postfix ++ & -- operators within the C++ Programming forums, part of the General Programming Boards category; Just wondering how the compiler knows the correct operator method to call. Code: #include <iostream> class COUNTER { public: COUNTER(); ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    11

    prefix/postfix ++ & -- operators

    Just wondering how the compiler knows the correct operator method to call.

    Code:
    #include <iostream>
    
    class COUNTER {
    public:
    	COUNTER();
    	~COUNTER();
    	int get_val() const;
    	void set_val(int x);
    	const COUNTER& operator ++();
    	const COUNTER operator ++(int flag);
    	const COUNTER& operator --();
    	const COUNTER operator --(int flag);
    private:
    	int val;
    };
    
    COUNTER::COUNTER(): val(0) {}  
    COUNTER::~COUNTER() {}  
    
    //methods
    int COUNTER::get_val() const { return val; }
    void COUNTER::set_val(int x) { val = x; }
    
    //overloaded operators
    const COUNTER& COUNTER::operator ++() {
    	++val;
    	return *this;
    }
    const COUNTER COUNTER::operator ++(int flag) {
    	COUNTER temp(*this);
    	++val;
    	return temp;
    }
    const COUNTER& COUNTER::operator --() {
    	--val;
    	return *this;
    }
    const COUNTER COUNTER::operator --(int flag) {
    	COUNTER temp(*this);
    	--val;
    	return temp;
    }
    
    int main (void) {
    	using namespace std;
    	COUNTER i,j;
    	cout << endl;
    	cout << "COUNTER i,j starting values" << endl;
    	cout << "i == " << i.get_val() << endl;
    	cout << "j == " << j.get_val() << endl;
    	cout << endl;
    	cout << "j = ++i" << endl;
    	j = ++i;
    	cout << "++i == " << i.get_val() << endl;
    	cout << "j == " << j.get_val() << endl;
    	cout << endl;
    	cout << "j = i++" << endl;
    	j = i++;
    	cout << "i++ == " << i.get_val() << endl;
    	cout << "j == " << j.get_val() << endl;
    	cout << endl;
    	i.set_val(5);
    	j.set_val(5);
    	cout << "Reset COUNTER i,j" << endl;
    	cout << "i == " << i.get_val() << endl;
    	cout << "j == " << j.get_val() << endl;
    	cout << endl;
    	cout << "j = --i" << endl;
    	j = -- i;
    	cout << "i == " << i.get_val() << endl;
    	cout << "j == " << j.get_val() << endl;
    	cout << endl;
    	cout << "j = i--" << endl;
    	j = i--;
    	cout << "i == " << i.get_val() << endl;
    	cout << "j == " << j.get_val() << endl;
    	cout << endl;
        return 0;
    }
    Without accually changing the body of the methods and just swapping the flags in the declaration and definition the result is not correct. Does the compiler just look for the flag and say without a doubt this is the postfix operator or is it more involved?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,339
    Yes, it just looks for the flag.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    11
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. Overloading fstream's << and >> operators
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2007, 03:17 AM
  4. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 06:35 PM

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