Detailed description of C ++ compilation and code Problems

Source: Internet
Author: User

For every employee in the software industry, developers, project managers, and testers must constantly adapt to this trend, I think C ++ compilation will only make our work easier and easier. I hope this article will teach you more things.

If this is the design problem of the database you purchased, you have nothing to do with it except for a better library), but you can better organize your own code, to minimize the re-compilation after the modified Code. This design will be better and more maintainability, as they demonstrate better conceptual separation.

Let's take a look at this typical object-oriented C ++ compiler example:

 
 
  1. Class Shape {
  2.  
  3. Public: // User Interface Using Shapes
  4.  
  5. Virtual void draw () const;
  6.  
  7. Virtual void rotate (int degrees );
  8.  
  9. //...
  10.  
  11. Protected: // common data (for implementers of Shapes)
  12.  
  13. Point center;
  14.  
  15. Color col;
  16.  
  17. //...
  18.  
  19. };
  20.  
  21.  
  22. Class Circle: public Shape {
  23.  
  24. Public:
  25.  
  26. Void draw () const;
  27.  
  28. Void rotate (int ){}
  29.  
  30. //...
  31.  
  32. Protected:
  33.  
  34. Int radius;
  35.  
  36. //...
  37.  
  38. };
  39.  
  40.  
  41. Class Triangle: public Shape {
  42.  
  43. Public:
  44.  
  45. Void draw () const;
  46.  
  47. Void rotate (int );
  48.  
  49. //...
  50.  
  51. Protected:
  52.  
  53. Point a, B, c;
  54.  
  55. //...
  56.  
  57. };

The design idea is that the user uses the public interface of Shape to manipulate them, while the implementation part of the derived classes such as Circle and Triangle shares the part represented by the protected member to implement implementation ). This is not an easy task: Determine which implementations are useful to all derived classes and share them.

Therefore, compared with the public interface, protected members often need to make much more changes. For example, although in theory "center" is an effective concept for all graphics, when you want to maintain the "center" of a triangle, it is a very troublesome thing-for a triangle, it makes sense to calculate this center only when it is actually needed.

  • Better understanding of C ++ programming learning and research
  • Elaborate on the relevant learning methods of C ++ for discussion and research
  • Waste collection defects in c ++
  • Learn and guide Visual C ++ development tools
  • Introduction to function overloading in C ++

The protected member may depend on the Implementation Details. the user of the Shape is translated as the user here, and the C ++ compilation of the Shape class is used, the same below) but it is not necessarily dependent on them. For example, many of them ?) The code for using Shape is logically unrelated to the color. However, due to the existence of the color definition in Shape, a bunch of complicated header files may be required, in combination with the operating system color concept.

When the protected part changes, the code that uses the Shape must be re-compiled-even if only the implementation part of the derived class can access the protected member. Therefore, information helpful to implementers in the base class becomes something as sensitive as the interface, and its existence leads to instability of the implementation part, unnecessary re-Compilation of user code when the implementation part changes), and include the header file unlimitedly in the user code because "Implementation-Related Information" requires them ). This is sometimes referred to as the "vulnerable base class problem" (brittle base class problem ).

An obvious solution is to ignore the "Implementation-Related Information" that is used like an interface in the base class ". In other words, you can use pure interfaces. That is to say, the interface is represented by abstract base classes:

 
 
  1. Class Shape {
  2.  
  3. Public: // User Interface Using Shapes
  4.  
  5. Virtual void draw ()Const=0;
  6.  
  7. Virtual void rotate (int degrees) = 0;
  8.  
  9. Virtual Point center ()Const=0;
  10.  
  11. //...
  12.  
  13.  
  14. // No data
  15.  
  16. };
  17.  
  18.  
  19. Class Circle: public Shape {
  20.  
  21. Public:
  22.  
  23. Void draw () const;
  24.  
  25. Void rotate (int ){}
  26.  
  27. Point center () const {return center ;}
  28.  
  29. //...
  30.  
  31. Protected:
  32.  
  33. Point cent;
  34.  
  35. Color col;
  36.  
  37. Int radius;
  38.  
  39. //...
  40.  
  41. };
  42.  
  43.  
  44. Class Triangle: public Shape {
  45.  
  46. Public:
  47.  
  48. Void draw () const;
  49.  
  50. Void rotate (int );
  51.  
  52. Point center () const;
  53.  
  54. //...
  55.  
  56. Protected:
  57.  
  58. Color col;
  59.  
  60. Point a, B, c;
  61.  
  62. //...
  63.  
  64. };

Now, the relationship between the changes in the implementation part of C ++ compilation and the derived class is isolated. I have seen this technology reduce the Compilation Time by several orders of magnitude.

Related Article

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.