Valid tive C ++ Note 1

Source: Internet
Author: User

I have read a part of Objective C ++ before, but I found that there is no actual programming in the future. So I will study it again and take notes. When you are idle, we often look at these terms and apply them to actual programming.
(1) clause 01 regards C ++ as a Language Federation
To understand C ++, you must understand its main sub-language. There are four in total:
C, Object-Orented C ++, Template C ++, STL
(2) clause 02 should be replaced with const, enum, and inline as much as possible # define
For example: # define VALUE 1, the mark name may not enter the mark table, so when you use this variable to generate a compilation error, the VALUE 1 is mentioned in the error message of the compiler, but there is no VALUE, which makes it impossible for you to locate the error accurately. Instead, using const to declare constants will solve this problem.

Class GamePlayer {
Static const int num = 5;
Int scores [num];
};

In legacy compilers, the preceding syntax may not be supported. They do not allow static members to obtain initial values in their declarative statements. If your compiler does not support assignment in declarative statements, it can be defined in the class implementation file. For example:

Class Test {
Static const int num;
// Declare it in the header file of Test
};
Const int Test: num = 5;
// In the Test implementation file

However, in the GamePlayer class, you must give the array scores a definite value. In this case, you can use the "the enum hack" compensation method. The theoretical basis is: "A value of the enumeration type can be used with ints." Therefore, GamePlayer can be defined as follows:

Class GamePlayer {
Enum {num = 5 };
Int sorces [num];
};

For the enum hack method, its behavior is similar to # define, but it can be included in the mark table, which can avoid the problem of poor location of compilation errors, and it is not like a const constant, a const value is acceptable, but an enum address cannot be obtained. So when you don't want others to get a pointer or reference pointing to an integer constant, enum can help you implement this constraint.
Using # define to implement macros similar to functions does not incur additional overhead caused by function calls. However, various traps and troubles often occur when using such macros. Therefore, inline is used to replace macro similar to function.
(3) clause 03: Use const whenever possible
Declaring something as const can help the compiler detect usage errors. const can be applied to objects in any scope, function parameters, function return types, and member function ontology.
The compiler enforces bitwiseconstness, but you should use "Conceptual constants" when writing programs"
When the const and non-const member functions have essentially equivalent implementations, the non-const version calls the const version to avoid code duplication.
(4) clause 04: determine that the object has been initialized before use
Reading uninitialized objects may lead to unclear behavior. On some platforms, the program may be terminated simply by reading uninitialized values.
Therefore, initialization should be performed first before any type is used, whether it is a built-in type or a custom type. For built-in types without any Members, we need to perform initialization manually. For custom types, the initialization task falls into the constructor. In addition, you also need to know the difference between value assignment and initialization. For example:

Class Test {
Int x;
Stringy;
Test (int x, string y ){}
};
Test: Test (){
This-> x = x; // this is a value assignment operation, not an initialization operation.
This-> y = y;
}

C ++ specifies that the initialization action of the object's member variables takes place before entering the constructor ontology. For example, the initialization of the member function of Test occurs before the default constructor is called. The best way to write the Test constructor is to replace the value assignment with the so-called member initialization list:

Test: Test (int x, string y): x (x), y (y)
// This initializes the member variables.
{
// Do other thing
}

In this way, the corresponding values are set for the member variables during initialization, without a value assignment operation. Improved efficiency.
C ++ has a very fixed "initialization order of members ". The base class is always earlier than the subclass initialization, while the class member variable Initialization is always in the order it declares.
C ++ does not explicitly define the initialization sequence of non-localstatic objects defined in different compilation units ". The solution to this problem is to move each non-local static object to its own exclusive function. These functions return a reference. You can call these static functions without directly involving these objects. This approach is based on the C ++ guarantee that the local static object in the function will be initialized when "this function is called, this object is first encountered. This method is used in singleton mode.

 

From cscmaker's column

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.