Click to See Complete Forum and Search --> : Is this possible? ;^)


zdf
June 20th, 2002, 04:56 AM
#include <ostream.h>
#include "main.h"

void main()
{
UINT i = 0;
+++++++i----++;
cout << i << endl;
}

sjeng
June 20th, 2002, 06:14 AM
Why don't you implement it and find out?
experience is the best teacher :)

Alexis Moshinsky
June 20th, 2002, 06:21 AM
until post increment yes (++++...i; ).

It is also possible to use microscope as hammer :D
The question is what for ?

zdf
June 20th, 2002, 06:39 AM
Originally posted by Alexis Moshinsky
until post increment yes (++++...i; ).

It is also possible to use microscope as hammer :D
The question is what for ?

What if UINT is a class in "main.h"? And... what if you overload some operators?

It's just a question. Does it make you think? :D

Alexis Moshinsky
June 20th, 2002, 06:47 AM
I do not know what for You prepare Yourself.

In my company if some young softhead write a class
named UINT, he/she will be fired immediately.

sjeng
June 20th, 2002, 07:01 AM
++++++ is no default operator and it wont be seen as a stack of operators, so you would have to define a custum operator

zdf
June 20th, 2002, 07:52 AM
It may be a stupid problem but it points out what Mr. Moshinsky said some time ago: “C++ is not only its own language. It is most powerful language of the world.” (At least among the programming-languages I know.:) )

A solution may be:


class UINT
{
public:
UINT( unsigned int ad = 0 ) { d = ad; }
UINT& operator++(int) { ++d; return *this; }
UINT& operator++() { ++d; return *this; }
UINT& operator+() { return *this; }
UINT& operator--(int) { --d; return *this; }
UINT& operator--() { --d; return *this; }
friend ostream& operator<<(ostream& os, UINT& ui) { os << ui.d; return os; }
protected:
unsigned int d;
};


Have a nice day!

Alexis Moshinsky
June 20th, 2002, 08:09 AM
About UINT i was just serious. Never give well known things
not standard meaning.

zdf
June 20th, 2002, 08:16 AM
Originally posted by Alexis Moshinsky
About UINT i was just serious. Never give well known things
not standard meaning.

Well, of course you are right! But I had to find a pretext to line up some signs otherwise you could not.

Have a nice day!

Paul McKenzie
June 20th, 2002, 08:28 AM
Originally posted by zdf

#include <ostream.h>
#include "main.h"

void main()
{
UINT i = 0;
+++++++i----++;
cout << i << endl;
}
Using an ANSI C++ compiler -- this wouldn't even compile. Even if you did have some header called ostream.h and main.h, and you did have a class called UINT, this code won't compile for the following reason:

// void main() This is an error
int main() // This is correct

So is this possible? Not with a conforming compiler. There are compilers now that flag it as an error, so I can't wait when VC++ is really ANSI compliant and starts to flag that line as an error. The surprised look on the faces of programmers will be a sight to see :)

Regards,

Paul McKenzie

zdf
June 20th, 2002, 08:33 AM
Originally posted by Paul McKenzie
Using an ANSI C++ compiler -- this wouldn't even compile. Even if you did have some header called ostream.h and main.h, and you did have a class called UINT, this code won't compile for the following reason:

// void main() This is an error
int main() // This is correct

So is this possible? Not with a conforming compiler. There are compilers now that flag it as an error, so I can't wait when VC++ is really ANSI compliant and starts to flag that line as an error. The surprised look on the faces of programmers will be a sight to see :)

Regards,

Paul McKenzie

Ooopssss! :eek:

Thank you!

Alexis Moshinsky
June 20th, 2002, 08:38 AM
The chain of preincrements should be Ok with any compiler.
The whole line will not compile nowhere.

zdf
June 20th, 2002, 09:00 AM
Originally posted by Alexis Moshinsky
The chain of preincrements should be Ok with any compiler.
The whole line will not compile nowhere.

I have already compiled with VC++. It works fine. The displayed value is 2. This is the code:


#include <ostream.h>

class UINT
{
public:
UINT( unsigned int ad = 0 ) { d = ad; }
UINT& operator++(int) { ++d; return *this; }
UINT& operator++() { ++d; return *this; }
UINT& operator+() { return *this; }
UINT& operator--(int) { --d; return *this; }
UINT& operator--() { --d; return *this; }
friend ostream& operator<<(ostream& os, UINT& ui) { os << ui.d; return os; }
protected:
unsigned int d;
};


void main()
{
UINT i = 0;
+++++++i----++;
cout << i << endl;
}

Alexis Moshinsky
June 20th, 2002, 09:46 AM
I meant of course predefinded data types, not user defined.

Nice evening to You too.

sandodo
June 20th, 2002, 10:33 PM
Normally when people see code like this: +++++++i----++;, he will be surprised. Following the standard way of doing as what the int does provided in C++ libraries is recommended, the better way is to add the const before the operator++ or etc to prevent operation like ++++++i----; the way of +++++++i----++; is good for exercise only. :)

const UINT& operator++(int) { ++d; return *this; }