FAQ 23.07 How are the prefix and postfix versions of operator distinguished?

FAQ 23.07 How are the prefix and postfix versions of operator++ distinguished?

They are distinguished by a dummy parameter.

The postfix version, i++, is called operator++ and has a dummy parameter of type int. The prefix version, ++i, is also called operator++, but it has no dummy parameters. Here's an example.

 #include <iostream> using namespace std; class Fred { public:   void operator++ ()    throw();   void operator++ (int) throw(); }; void Fred::operator++ ()    throw() { cout << "prefix: ++i\n"; } void Fred::operator++ (int) throw() { cout << "postfix: i++\n"; } int main() {   Fred i;   ++i;   i++; } 

The output is

 prefix: ++i postfix: i++ 

The two versions of operator-- are similar: the prefix version takes no parameters and the postfix version takes a single parameter of type int.



C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net