Cognition of several key knowledge points in C + +

Source: Internet
Author: User
Tags aliases

The understanding of structure in C + +

1. Usage of typedef
In the C + + language, typedef is commonly used to define an alias for an identifier and a keyword , which is part of the language compilation process, but it does not actually allocate memory space.

Instance Image: typedef int INT; typedef int ARRAY[10]; typedef (INT*) PINT;

typedef can enhance the readability of the program, as well as the flexibility of identifiers, but it also has a "non-intuitive" and other shortcomings.

2. #define的用法
#define为一 a macro definition statement, which is typically used to define constants (including parametric and parametric), and to implement those "seemingly benign, behind-the-scenes" macros , which are not itself in the process of compiling, but before ( preprocessing) has been completed, but it is therefore difficult to identify potential errors and other code maintenance issues.

Its example is: #define INT int #define TRUE 1 #define ADD (A, B) ((a) + ()); #define LOOP_10 for (int i=0; i<10; i++)

In article 1 of effective C + + of Scott Meyer, there is an analysis of the drawbacks of the # define statement , as well as a good alternative, as you can see.

3. The difference between typedef and # define

From the above concepts can also be basically clear, TypeDef is only to increase the readability of the identifier for the new name (just an alias), and # define was originally in C in order to define constants, to the C++,const, enum, The advent of inline makes it a tool for aliases as well. Sometimes it is easy to figure out with the typedef which is the best, such as the # define int int Such statements, with a typedef can be done, with which good?

I contend with TypeDef, because this statement is illegal in many of the earlier C compilers, but today's compilers are expanded.

To be as compatible as possible, it is common to follow #define定义 "readable" constants and the tasks of some macro statements, whereas TypeDef is often used to define keywords, lengthy types of aliases .

A macro definition is simply a string substitution (in-place extension), whereas a typedef is not an in-place extension, and its new name has a certain encapsulation so that the newly named identifier has the ability to define variables more easily.

See the third line of the first big dot above: typedef (int*) PINT; and the following line: #define PINT2 int*

 same effect? It's different! See the difference in practice : PINT A, B, the effect with int *a; An int *b that defines two integer pointer variables. And pINT2 A, B; the effect is the same as int *a; Indicates that an integer pointer variable A and integer variable B are defined. Note: There is also a line at the end of the number of the difference Oh!

The #define macro is used to replace the above as: int *a, B is obviously only the type of int before B.

3. The Const base

If the const keyword does not involve pointers, we understand very well that the following is a case involving pointers :
int b = 500;

Const int* A = &b; [1]

int const *A = &b; [2]

int* const A = &b; [3]

Const int* Const A = &b; [4]

If you can distinguish between the above four situations, then, congratulations, you have taken a welcome step. I don't know, it doesn't matter, we can refer to the practice on effective C + + ITEM21, if the const is on the left side of the asterisk, the const is used to decorate the variable pointed to by the pointer, that is, the pointer is constant, if the const is on the right side of the asterisk, A const is a modifier of the pointer itself, i.e. the pointer itself is a constant. Thus, [1] and [2] are the same, all pointers to the content is constant (const placed in the position of the variable declarator independent), in this case, the content is not allowed to change operations, such as *a = 3; [3] for the pointer itself is a constant, and the pointer to the content is not a constant, In this case, you cannot make a change to the pointer itself, such as a++ is wrong; [4] is a constant for both the pointer itself and the pointed content.
Another powerful feature of the const is its application in function declarations. In a function declaration, a const can modify the return value of a function, or a parameter, and for a member function, it can be decorated as an entire function. There are a few situations where the following is a gradual explanation of usage:

a& operator= (const a& A); void Fun0 (const A * A); void fun1 () const; FUN1 () is a class member function Const A FUN2 ();

Cognition of several key knowledge points in C + +

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.