Effective C + + reading notes (a) Make yourself accustomed to c++__c++

Source: Internet
Author: User
Tags bitwise constant
1 Make yourself accustomed to C + + clause 01: View C + + as a language federation
View C + + as a federation oflanguages

–c. In the final analysis C + + is still based on C. Chunks (blocks), statements (statements), preprocessor (preprocessor), built-in data types (built-in data types), arrays (arrays), pointers (pointers), all come from C. Limitations: Non-templates non-exceptions non-overloading –object-oriented C + +. Classes (including constructors and destructors), Encapsulation (encapsulation), Inheritance (Inheritace), polymorphism (polymorphism), virtual function (late bundle) ... Wait a minute. –template C + +. Generic Programming section. –stl.

C + + efficient programming rules vary depending on the situation, depending on which part of C + + you are using.

clause 02: Try to replace #define with Const,enum,inline
Prefer Consts,enums,and inlines to #define
– Replace the preprocessor with the compiler. That is, as little as possible with pretreatment
– #define may not enter symbol table.

Const: • Constant pointer (constant pointers) -Example: Invariant char*-based string (preferably with string)

Const char* Const AUTHORNAME = "Scott Meyers";

class Exclusive Constants – To ensure that this constant has at most one entity, you must make it a static member:
Classgameplayer {
Private:
   static const int numturns = 5;   Constant declaration
};


– Note: You can declare and use them without having to provide a definition as long as you do not take its address. – If you take the address of a class-specific constant or if the compiler complains, you must provide a definition.

such as: const intgameplayer::numturns;

Because the class constant has an initial value (static) when it is declared, you do not need to set the initial value for the definition. If the compiler does not allow this, you can use the definition outside the declaration to give the initial value. such as: const int gameplayer::numturns = 5; If you still have an error, you can choose to use the enum keyword.

such as: enum{numturns = 5}; template Inline instead of macros

#define CALL_WITH_MAX (A,b)    F ((a) > (b))? (a):(B))
int a = 5,B = 0
Call_with_max (++a,b);   A is accumulated two times
Call_with_max (++a,b+10);//a is cumulative once


Template<typename t>
inline void Callwithmax (cosnt t &a, cosnt t &b)
{
  F (a > B a:b);
}


Enum

1 more like #define rather than Const. It is illegal to take an enum's address, so you can prevent pointer or reference from pointing to one of your integer constants.

The basic technology of 2 template metaprogramming

for simple constants, it is best to replace the #defines with a const object or enums; For macros that resemble functions, it is a good idea to replace #defines with the inline function instead.

clause 03: Use const as much as possible
Use const whenever possible
– If the keyword const appears to the left of the asterisk, the indicated type is a constant, and if it appears to the right of the asterisk, the pointer itself is a constant, and if both sides of the asterisk indicate the type and the pointer are both constants. the most representative of –const is the application of function declaration. The const can be associated with function return values, parameters, functions themselves. – The function returns a constant value that can often reduce the number of accidents caused by customer errors without abandoning security and efficiency.

If (A * b = c) –const member functions • 1 They make the class interface easier to understand. • 2 They make it possible to "manipulate a const object". – If the return type of a function is a built-in type, it is not possible to get rid of the function return value. –bitwise constness (physical constness)

• A member function can be said to be const only if it does not change any member variables of the object (except static). That is, it does not change any bit (bit) within an object. –logical constness

• A const member function can modify some bits within the object it handles, but only if the client does not detect it. The –mutable (variable) keyword can release the bitwise constness constraint of the non-static member variable; – Avoid duplication in const and NON-CONST member functions

The const member function calls the Non-const member function is an error behavior because the object may be altered accordingly. –const_cast

• Usage:const_cast<type_id> (expression)

– the operator is used to modify the const or volatile property of a type.

– The constant pointer is converted to a very measured pointer and still points to the original object;

– The constant reference is converted to a very literal reference and still points to the original object;

– The constant object is converted to a very object; –static_cast

• Usage:static_cast<type_id> (expression)

• This operator converts expression to a type-id type, but does not have run-time type checking to guarantee the security of the conversion.

declaring something as const can help the compiler detect incorrect usage. Const can be applied to any scope of objects, function parameters, function return type, member function ontology; the compiler enforces bitwise constness, but you should use "Conceptual constants" When you write your program (conceptual constness); when cosnt and NON-CONST member functions have substantial equivalence implementations, the NON-CONST version calls the const version to avoid code duplication.
clause 04: Determines that an object has been initialized before it is used
Make sure that objects are initialized Beforethey ' re used
-reading an uninitialized value can cause ambiguous behavior.

the initialization of the L object must occur when it does not occur.

– for built-in types that do not have any members, you must do this manually.

– for anything other than a built-in type, the initialization responsibility falls on the constructor (constructors). Make sure that each constructor initializes each member of the object.

–c++ stipulates that the initialization of an object's member variable occurs before it enters the constructor ontology.

– The initialization list is exempt from calling the default constructor and then calling the assignment operator, just one copy of the constructor. more efficient.

– The best method for constructing a constructor is to use the Member initialization list (Members initialization table) as follows:

Abentry::abentry (char&name,char& address,list &phones)
 : thename (name), theaddress (address), Thephones (phones)
{
...
}

• The compiler automatically invokes the default constructor for the member variables of the user-defined type (user-definedtypes)---if those member variables are not specified in the member initialization list. • Member variables are const or references, they must have initial values and cannot be assigned. C++ has a very fixed "member initialization order." Base classes is initialized earlier than its derived classes, and class's member variables are always initialized in their declaration order. the static object, whose valid time is from being constructed until the end of the program, so that the stack and heap-based objects are excluded. –c++ The initialization relative order for "non-localstatic objects within different compilation units" is not clearly defined. • Small method: Place each Non-localstatic object within its own proprietary function (which is declared static within this function) that returns a reference to the object it contains.

• Manual initialization of built-in objects because C + + does not guarantee initialization of them; • Constructors are best to use the member initialization list instead of using assignment operations within the constructor ontology. Initializes the member variables listed in the list that should be in the same order as they are in the class; • Replace the non-local static object with the local static object to exempt the "initialization order across compilation units" issue.

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.