Hi Jerry,
Moved this from the C echo.
::> HB> I wonder if all those parenthesis are necessary? How would the
::> HB> compiler parse "x++++++++++++++"?
::> It would tokenize it as:
::> x++ ++ ++ ++ ++ ...
::> However, as soon as the parser encountered the second ++, it'd give you
::> an error something like "Lvalue required" - `x' is an Lvalue, meaning
::> the ++ can modify it. However, the result of `x++' is NOT an lvalue,
::> meaning the next `++' can't modify it, leading to an error.
Ok, I just bypassed the problem using a class. Here's the sample code I
mentioned in the C echo:
===========test1.cpp===============
#include
class test
{
private:
int x;
public:
test(){x=0;}
test& operator++(int){x++; return *this;};
friend ostream& operator<<(ostream& os, test& t);
};
ostream& operator<<(ostream& os, test& t){os << t.x; return os;};
int main(int, char *[])
{
test tst;
cout << "x = " << tst << endl;
tst++;
cout << "x = " << tst << endl;
(tst++)++;
cout << "x = " << tst << endl;
tst++++;
cout << "x = " << tst << endl;
return 0;
}
==========end test1.cpp=============
It compiled and worked correctly. The output was:
x = 0
x = 1
x = 3
x = 5
So it CAN be done, just not in C or C/C++ with the built-in types.
# Herbert Bushong harchon@centuryinter.net [TEAM OS/2]
- Blackbeard's BBS Intelec: 239:600/0
+ Fido: 1:19/19 http://www.win.net/eunicecity/stltcc/hbush/
---
RM 1.31 2508 SYSOP: The guy laughing at your typing.
---------------
* Origin: Blackbeard's BBS - Ville Platte, LA - 318-468-3385 (1:19/19)
|