Code: blocks tips. 6-let the compiler read more books :)

Source: Internet
Author: User

Although traditional paper books seem increasingly unpopular ......

But I personally like reading paper books. Why?

Because I made a cup of coffee, I pulled it to the swivel chair and sat down half-lying. I held C ++ books that were so thick that I could defend myself, and kept myself in the book room, the feeling is that you have already become a senior intellectual. Another situation is that you have to turn on your computer and stare at the screen ...... Alas, I feel like nothing. I can only return to reality and find myself a poor programmer.

 

A while ago, I flew back to Xiamen from Qingdao, where I got a copy of the People's Daily Overseas Edition (in English only ), sitting in the middle of the box is very lazy to read-in fact, it is very boring-several times move fingers on words that do not understand, think there will be a small form of pop-up translation content ...... In any case, the Force installation effect is very good. The two beautiful women on the side directly cast their eyes on my admiration. The one on the right left me two QQ accounts and talked with me all the way to Xiamen, finally, change the mobile phone number ......

 

This is the power of knowledge! : B

 

The same is true for compilers! Think of Scott Meyers, one of the world's top C ++ development authorities) compared with a compiler that has read these two books, compiler C ++ and more powerful C ++.

This is all nonsense.

Open code: blocks (using the Chinese Language Pack Interface), choose "Settings"> "compiler and Debugger" from the main menu, and select the GCC compiler. Then configure its compiler options:

Enable valid tive-C ++ warnings ....

This configuration allows all subsequent C ++ projects created in code: blocks using the GCC compiler to use this option. If you only want to use it in a specific project, you can configure it in a specific project (after opening the project, Main Menu: Project-> build option ).

 

What are the benefits of this option? Let's test ourselves first:

Code:
  1. # Include <iostream>
  2. Using namespace STD;
  3. Class person
  4. {
  5. Public:
  6. Virtual void say ()
  7. {
  8. Cout <"I am a person." <Endl;
  9. }
  10. ~ Person ()
  11. {
  12. Cout <"bye-bye person." <Endl;
  13. }
  14. };
  15. Class beauty: Public Person
  16. {
  17. Public:
  18. Beauty ()
  19. : _ P (New INT)
  20. {
  21. }
  22. ~ Beauty ()
  23. {
  24. Delete _ P;
  25. }
  26. Virtual void say ()
  27. {
  28. Cout <"I am a beauty." <Endl;
  29. }
  30. PRIVATE:
  31. Int * _ P;
  32. };
  33. Int main ()
  34. {
  35. Return 0;
  36. }

What are the design risks of the above Code? The syntax is certainly no problem. A compiler that has not read books will certainly compile the above code in an obedient manner.

But a compiler that has read a book will give the following warning with simple translation:

 

| === Temp4book, debug === |

Main. cpp | 7 | warning: 'class person 'has virtual functions and accessible non-virtual destructor

(Row 3, class person has a virtual function, but its destructor are not virtual! Terrible design error)

Main. cpp | 21 | warning: 'class beauty' has pointer data members

(Row 3, class beauty has a pointer data member, next line ......)

Main. cpp | 21 | warning: but does not override 'beauty (const beauty &)'

(However, you have not customized the copy constructor for beauty !)

Main. cpp | 21 | warning: Or 'operator = (const beauty &)'

(Similarly, it does not overload its value assignment operator !)

Main. cpp | 21 | warning: 'class beauty' has virtual functions and accessible non-virtual destructor

(Row 3, class beauty also has a virtual function, but its destructor are not virtual! The reason lies in its base class .)

| |=== Built: 0 errors, 5 warnings ===|

 

Do you not believe the importance of these warnings? You can buy the two books mentioned above, which are published in China. Here is a simple example:

Code:
  1. Void test ()
  2. {
  3. Beauty B1;
  4. Beauty b2 = b1;
  5. }
A serious problem has occurred. Do you know where the problem is? Let's test a question: http://student.csdn.net/space.php? Do = Question & AC = detail> qid = 1839

-------------------------------------------------------------------------------

If you want to communicate with me, please click the following link to become my friend: http://student.csdn.net/invite.php? U= 112600 & C = f635b3cf130f350c

 

 

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.