Valid C ++ Clause 1 and Clause 2

Source: Internet
Author: User

Some people say that C ++ programmers can be divided into two types: those who have read valid C ++ and those who have not read them. The third edition of Scott Meyers, the world's top C ++ master, is indeed worthy of such a comment.

This book does not tell you what the C ++ language is and how to use the C ++ language, but from the perspective of an experienced C ++ master: How to quickly write robust, efficient, stable, easy to transplant, and easy to reuse C ++ programs.

There are 55 terms in this note. The usage experience and programming principles of C ++ are introduced from multiple perspectives and updated every day.

 

Clause 01: regard C ++ as a Language Federation

These terms remind readers that C ++ is no longer a single language, but should be considered a federation composed of relevant languages. In terms of language form, it is a multiparadigm programminglanguage that supports both procedural and object-oriented programming languages) function Form (functional), generic form (generic), metaprogramming language, these capabilities and elasticity make C ++ an unmatched tool. To understand C ++, you must know its main sub-language. Fortunately, there are only the following four languages in total. In terms of language types, it consists of several languages:

(1) c. In the end, C ++ is still based on C. Block (blocks), statement (statements), Preprocessor (Preprocessor), built-in data type (built-in data types), array (arrays), pointer (pointers) and so on.

(2) object-oriented C ++. This part is C with classes: classes (including constructor and destructor), encapsulation (encapsulation), inheritance (inheritance), polymorphism (polymorphism), virtual functions (dynamic binding )......

(3) template C ++. This is the general programming (generic programming) part of c ++, and is also the least experienced part of most programmers. Template-related considerations and designs have been filled with the entire c ++. In fact, due to the powerful power of templates, they bring a brand new programming model (programming paradigm), the so-called templatemetaprogramming (TMP, template metaprogramming)

(4) STL. STL is a template library, which is a very special one. It works closely with and coordinates container (containers), iterator (iterators), algorithm (algorithms), and function object conventions.

Remember these four languages. Don't be surprised when you switch from one language to another, resulting in an efficient programming code that requires you to change your strategy. For example, the built-in (c-like) language pass-by-value is generally more efficient than pass-by-reference, when you move from C part of C ++ to object-oriented C ++, because of the existence of user-defined constructor and destructor, pass-by-reference-to-const is always better. This is especially true when using template C ++, so you do not even know the type of the object to be processed. However, once you enter STL, you will understand that, both the iterator and function object are created on the C pointer, so for the STL iterator and function object, the old c
The pass-by-value Code applies again.

 

Clause 02: replace const, Enum, and inline as much as possible # define

This article discusses the problems brought about by # define in C language in C ++ programming and provides alternative solutions.

Macro definition # define in C language is just a simple replacement. It will cause trouble for program debugging and efficiency. In C ++, const, Enum, and inline are recommended to replace # define; however, with consts, enums, and inlines, our demand for pre-processors (especially # define) is reduced, but not completely eliminated. # Include is still a necessity, while # ifdef/# ifndef continues to play an important role in controlling compilation. The pre-processor is not yet fully retired.

# Define Definition

#define ASPECT_RATIO 1.653

Const Definition

const double AspectRatio=1.653

Enum Definition

enum{NumTurns=5};

Inline Definition

template<typename T>inline void callWithMax(const T& a,const T& b){   f(a>b?a:b);}

When we replace # defines with constants, there are two special cases worth mentioning. The first is to define the constant pointer:

const std::string authorName="Scott Meyers";

The second one is the exclusive class constant. To limit the scope of a constant to a class, you must make it a member of the class. To ensure that a constant has at most one entity, you must make it a static member.

Class gameplayer {PRIVATE: static const int numturns = 5; int scores [numturns];} // If the compiler does not allow you to do this, you can use "The Enum hack" to compensate, enum {numturns = 5 };

Remember:

For constants, it is best to replace # defines with a const object or enum.

Replace # defines

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.