Chapter One make yourself accustomed c++accustoming yourself to C + +
Article 01: Treat C + + as a language federal
View C + + as a federation of Languages
At first C + + was just a few object-oriented features, but as the language matured, it began to accept various concepts, features, and strategies that differed from C with classes. Exception brings different approaches to the structuring of functions, templates introduces a new way of designing thinking, and STL defines stretching practices that have not been seen before.
The
simplest approach is to treat C + + as a federation of related languages rather than a single language. C + + mainly has the following four sub-languages:
1.C. C + + is based on C. block blocks, statement statements, preprocessor preprocessor, built-in data type built-in, array types, pointer arrays, etc. from C. Many times C + + The solution to the problem is a higher-level C solution, but in C + + C components work, high-efficiency programming reflects the limitations of the C language: no template, no exception, no overloading ...
2.object-oriented C + +.This is part of C with classes: classes (including constructors and destructors), Encapsulation (encapsulation), Inheritance (inheritance), polymorphism (polymorphism), virtual function (dynamic binding) ... Wait a minute.
3.Template C + +.This is the generic programming (generic programming) part of C + +.
4.STL.STL is a templates library. It has excellent close coordination with the containers of container, iterator iterators, algorithm algorithms and function object objects.
When switching from one language to another, efficient programming requires a change of strategy. For example, Pass-by-value is usually more efficient than pass-by-reference for built-in types, but when moving from C part of C + + to object-oriented C + +, Because of the existence of user-defined constructors and destructors, Pass-by-reference-to-const is often better. This is especially true when using template C + +. However, once you enter STL, iterators and function objects are created on the C pointer, So for STL iterators and Function objects, the pass-by-value of C is again applicable.
Note: The C + + efficient programming code varies depending on which part of C + + is used.
Clause 02: Try to replace the #define with Const,enum,inline
Prefer Consts,enums,and inlines to #define
It might be better to replace the preprocessor with a compiler instead, because maybe # define is not considered part of the language, and that's where the problem is:
#define Aspect_ratio 1.653
The token name Aspect_ratio may never have been seen by the compiler, and it may have been removed by the preprocessor before the compiler began processing the source code. So aspect_ratio may not have entered the symbol table. So when you apply this constant but get a compilation error, may be confusing because this error message may refer to 1.653 rather than aspect_ratio.
The workaround is to replace the above macro (#define) with a constant:
Const double aspectratio = 1.653;
As a language constant, Aspectratio may be seen by the compiler and must be inside the tick table. Also for floating-point constants, using constants may result in smaller amounts of code than using # define, because the preprocessor "blindly aspect_ the macro name Ratio Replace with 1.653 "may result in multiple copies of the target code 1.653, which is not the case if you use a constant instead.
When you replace a # define with a constant, there are two special cases worth saying:
The first is to define a constant pointer (constant pointers). Since the constant definition is usually placed in the header file (so that it is included in a different source code), it is necessary to declare the pointer as Const. For example, to define a constant char*-base string within a file, you must write Const two times:
const char *const authorname = "Scott Meyers";
With regard to the meaning and use of const (especially when it is combined with pointers), Clause 3 has a complete discussion. String objects are usually char*-base than their predecessors, so the above authorname are often defined as better:
Const std::string AuthorName ("Scott Meyers");
The second noteworthy is class-specific constants. In order to limit the scope of a constant to class, you must make it a member of class, and to make sure that the variable has at most one copy of the entity, you must make it a static member:
Class Gameplayer {private: static const int numturns = 5; int scores[numturns];};
However, what is seen is the Numturn declarative rather than the definition. C + + usually requires a definition for anything you use, but special handling is required if it is a class-specific constant that is static and is an integer type. As long as they don't take their addresses, They can be declared and used without the need to provide a definition. However, if you remove the address of a class-specific constant, you must provide the following definition:
const int Gameplayer::numturns;
Since the class constant has already obtained the initial value at the time of declaration, it is not possible to set the initial value.
Note:
For simple variables, it is best to replace the # define with a const object or enums.
For macros that resemble functions, it is best to replace the # define with the inline function instead.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Effective c++--clause 1 and Clause 2 (1th chapter)