The evil C + +

Source: Internet
Author: User

Once saw a very interesting article, reproduced today. Sorry is not to find the most original version, it is a pity.

----------Gorgeous split-line----------

Linus once (September 2007) in the newsgroup gmane.comp.version-control.git and a Microsoft Engineer (Dmitry Kakurin) had a dispute with C or C + +, The Microsoft engineer at the time was primarily working on the git Windows version, but he found that Git's source code was written in C rather than C + +, so he (Dmitry Kakurin) posted a sign of dissatisfaction with Linux in the Linux community, and the language was straightforward:

Pure C as opposed to C + +. No idea why. Please don ' t talk about portability, it's BS. (plain C, not C + +, don't know why, please don't tell me it's for transplant, it's all nonsense.) )

Linux father Linus Torvalds immediately keep abreast, in the post, Linus words very direct, directly shows that C + + is a very scary language, he said in the post:

*you* is full of bullshit. C + + is a horrible language. It's made more horrible by the fact, a lot of substandard programmers use it. (You're the one who's totally talking nonsense. C + + is a scary language, and more frightening than it is that many unqualified programmers are using it.

Linus this point of view I agree, I personally also a few years ago, "the STL string class to copy" and some subsequent articles expressed that C + + is indeed not a very mature language, this view has always been around me. This is because its learning costs are too high, the compiler and the class behind you do a lot of things you do not know, and, C + + very easy to error and a lot of unexpected problems occur.

Of course, this article is not about continuing to denounce C + +, nor is it a review of previous events. We only talk about technology here. Yesterday, I saw an example of an evil C + + on the Internet, here to give you a share, let us look at the C + + programming language of the horror and evil side. The following example is more sinister than the "#define private public".

Take a look at the code below, can you tell me what it will output? (Note the line highlighted in the main function)

  1. #include <iostream>
  2. #include <vector>
  3. typedef INT UINT4;
  4. Using namespace std;
  5. Class Hack
  6. {
  7. };
  8. hack& operator< (Hack &a, Hack &b)
  9. {
  10. Std::cerr << "less than operator \ n";
  11. return A;
  12. }
  13. hack& operator> (Hack &a, Hack &b)
  14. {
  15. Std::cerr << "greater than operator \ n";
  16. return A;
  17. }
  18. int main (int argc, char * * argv)
  19. {
  20. Hack Vector;
  21. Hack UINT4;
  22. Hack foo;
  23. vector<uint4> foo;
  24. return (0);
  25. }

Yes, if the above code is only looking at the "vector<uint4> foo" in the main function, you will find it very familiar, however, it is not so simple, we can see that vectors, UINT4 and Foo are examples of the hack class, which is the beginning of evil, The two angle brackets < > are two operators, greater than and less than, and these two operators are overloaded. In fact, the real statement is:

    1. vector.operator< (UNIT4) .operator> (foo);

So, everything is in accordance with our C + + specifications and syntax, and the natural program can be successfully compiled (at least, there is no problem on my g++). The result of the entire program is naturally:

小于操作符
大于操作符

Yes, if you know everything about C + +, you will naturally not be surprised by this program. Such things are not uncommon in the C + + world, it is not a difficult thing to mess up the code of C + +, there are countless tricks, just look at "6 Sick C language Hello World program" You know, and there is a simple tutorial "How to encrypt/confuse C source code" Tell you a few simple ways.

So, if you see "vector<uint4> foo" in your program one day, do you think it's just an illusion?

Reproduced:

http://devbean.blog.51cto.com/448512/320210

The evil C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.