CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jun 2002
    Posts
    224

    Talking Is this possible? ;^)


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

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

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  2. #2
    Join Date
    Jun 2002
    Location
    Maastricht / The Netherlands
    Posts
    79
    Why don't you implement it and find out?
    experience is the best teacher
    The search feature of this forum is on the top right of the page. It might be a very good idea to use it.
    ----------------
    To retain respect for sausages and laws, one must not watch them in the making.
    - Otto von Bismarck (1815-1898) -

  3. #3
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    until post increment yes (++++...i; ).

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

  4. #4
    Join Date
    Jun 2002
    Posts
    224
    Originally posted by Alexis Moshinsky
    until post increment yes (++++...i; ).

    It is also possible to use microscope as hammer
    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?
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  5. #5
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    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.

  6. #6
    Join Date
    Jun 2002
    Location
    Maastricht / The Netherlands
    Posts
    79
    ++++++ is no default operator and it wont be seen as a stack of operators, so you would have to define a custum operator
    The search feature of this forum is on the top right of the page. It might be a very good idea to use it.
    ----------------
    To retain respect for sausages and laws, one must not watch them in the making.
    - Otto von Bismarck (1815-1898) -

  7. #7
    Join Date
    Jun 2002
    Posts
    224
    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!
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  8. #8
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    About UINT i was just serious. Never give well known things
    not standard meaning.

  9. #9
    Join Date
    Jun 2002
    Posts
    224
    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!
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Is this possible? ;^)

    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:
    Code:
    // 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

  11. #11
    Join Date
    Jun 2002
    Posts
    224

    Re: Re: Is this possible? ;^)

    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:
    Code:
    // 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!

    Thank you!
    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  12. #12
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    The chain of preincrements should be Ok with any compiler.
    The whole line will not compile nowhere.

  13. #13
    Join Date
    Jun 2002
    Posts
    224
    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;
    }

    ZDF

    What is good is twice as good if it's simple.
    "Make it simple" is a complex task.

  14. #14
    Join Date
    Mar 2002
    Location
    Israel
    Posts
    187
    I meant of course predefinded data types, not user defined.

    Nice evening to You too.

  15. #15
    Join Date
    Jun 2002
    Posts
    137

    Cool

    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; }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured