Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to transition from C language to C++const

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I would like to talk to you about how to transition from C language to C++const. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something from this article.

1. Define constant

1.1 the method of defining constants in C language

In this series of C language starting from scratch, we talked about how C language defines constants. For students who have not seen it, please refer to: C language starts from scratch (5)-constant & variable

I'm not going to dwell on why I want to define a constant, and I'm going to focus on what's wrong with that definition. There are often such interview questions: please write the execution result of the following code:

# include # define SUM 5 + 1 * void main () {int a = 2 * void ("% d", a);}

People often answer 12, but the result is 11. If you don't believe it, you can run it on the computer.

Why is it wrong? because the constant defined by # define is a pseudo-constant, it does the original character substitution when it participates in the compilation. That is, 2 * SUM is supposed to be in the eyes of the compiler.

Int a = 2 * 5 + 1

If your intention is to get 12, then the definition should say:

# define SUM (5 + 1)

Many people have made such classic mistakes, although we all know the truth, but they always fall into this pit because of carelessness.

Therefore, C++ introduces const constant to solve this problem completely. Later, some C language compilers also began to support the use of const, which fully illustrates its value.

1.2 const constant

In C++, we define constants in the following form:

Const int MONTH = 1210 Const int SUM = 5 + 1

Strictly speaking, the const constant should be called a "constant", which defines a variable whose value will not be modified.

For the sake of uniform code style, we are still used to naming const constants in all uppercase letters.

Characteristics

There are two biggest differences between const constants and ordinary constants:

The value cannot be changed.

Can be used as a definition of array size

For example:

Const int MAX = 10 int arr [MAX] = {0}; for (int I = 0 int I)

< MAX; i++){ // Do something} 1.3 作用范围 const定义的常量的作用域类似与static,只能被当前文件访问。如果想在其他文件中使用该如何写呢? // file1const int MAX = 10;// file 2extern const int MAX; 不过并不推荐这么使用,还是建议大家把const定义写在头文件中,在需要的文件中包含这个头文件。 2. 指针与const const的修饰特点是修饰离它最近的部分。它一般有两种用法。 2.1 指向const变量的指针 让指针指向一个const对象,防止指针修改所指向的值。 int age = 30;const int* ptr = &age; 这段代码定义了一个指针ptr,它指向一个const int类型的数据,不可修改。 *ptr += 1; // 报错 cin >

> * ptr; / / error report

Both ways of writing are illegal.

Note: it can still be modified with the age variable.

2.2 const pointer

Declare the pointer itself as a constant to prevent the pointer from changing its position

Int a = 3int * const p = & a Ting pairing; / / error

Note: only const pointers can point to const variables, for example:

Const int a = 9 Const int* p = & a; / / correct int* ptr = & a; / / error

Special use:

Const int* const p = & a

This sentence means that the contents of the pointer variable and the address pointed to are immutable.

3. Function and const

3.1 const parameters

If you want the parameters not to be modified inside the function, you can modify them with const, as follows:

Void fun (const int a) {aquired operation; / / illegal operation}

Because an is modified as a constant by const, an error will be reported if it is axed + again.

The purpose of this writing is only to limit the modification of parameters within the function, and now more and more people like to do this:

Void fun (int a) {const int& b = a; illegal operations; / / illegal operations}

The effect is exactly the same.

3.2 const return value

If the return value of the function is a basic data type, it doesn't make sense to decorate it with const. For example:

Const int fun () {return 1;}

The return value of the fun () function cannot be modified as a "left value", because no one will use it this way:

Fun () = 2

The compiler will also filter out this writing first.

In general, const is only used to modify functions that return an object whose value is a class. For example:

Class A {public: a () {return I = 0;} A (int I): Massii (I) {} void Modify (int I) {return I = I;} private: int massii;}; A GetA () {return A (1);} const A GetConstsA () {return A (1);} void Update (A & a) {a.Modify (2);} void Update2 (const A & a) {A m = a M.Modify (2);} int main () {GetA () = A (1); / / correct GetA (). Modify (5); / / correct GetConstsA () = A (1); / / error GetConstsA (). Modify (); / / error Update (GetA ()); / / correct Update (GetConstsA ()); / / error Update2 (GetConstsA ()); / / correct return 0;}

Can you understand the mystery? To sum up, if the return value of the const decoration is an object of the class, then:

This return value cannot be left (assigned to the left of the equal sign or called its member function)

The alias of this return value must also be modified by const

4. infer other things from one fact

Knowing that the general parameters and return values are modified by const, we should be able to deduce the case of const modified pointer parameters and return values. Let's use a piece of code to look at the errors that are easy to make.

Void fun1 (int* p) {/ / Do nothing} void fun2 (const int* cp) {* cp = 3; / error int I = * cp; int* ip2 = cp; / / error} const char* fun3 () {return "result of function fun3 ()";} const int* const fun4 () {static int i; return & I;} int main () {int x = 0; int* p = & x; const int* cp = & x; fun1 (p) Fun1 (cp); / / error fun2 (p); fun2 (cp); char* cp = fun3 (); / / error const char* ccp = fun3 (); int* p2 = fun4 (); / / error const int* const ccp = fun4 (); const int* cp2 = fun4 (); * fun4 () = 1; / / error return 0;}

The various assignments in this program are in fact fully in line with the principles introduced in part 2. In the process of passing parameters and assigning values, you should pay attention to:

When the pointer content is modified by const, * p cannot be modified.

When pointer content is modified by const, it cannot be assigned to a pointer that is not const.

When pointer variables and contents are modified by const, only pointers in the same case can be assigned values

After reading the above, do you have any further understanding of how to transition from C language to C++const? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report