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

What are the features referenced by C++

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

Share

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

In this article, the editor introduces in detail "what are the characteristics of C++ citation". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what are the characteristics of C++ citation" can help you solve your doubts. Let's follow the editor's way of thinking to learn new knowledge.

I. the concept of citation

A reference is not a new definition of a variable, but an alias to an existing variable. In syntax understanding, the program does not open up memory space for the reference variable, it shares the same memory space as the variable it references.

For example, Li Kui is called "iron cow" at home and "black whirlwind" in rivers and lakes.

Type & reference variable name (object name) = reference entity

Int main () {/ / has a space a, followed by three aliases b, c, d int a = 10; int& b = a; int& c = a; int& d = b; / / char& d = a / / err, the reference type and the reference entity are not of the same type (there is a dispute here-char a = b [int type], leave suspense, which will be answered below) / / will be modified c = 20; d = 30; return 0;}

Be careful

The reference type must be the same type as the reference entity

Pay attention to the distinction between'& 'taking address symbols

II. Reference characteristics

References must be initialized when they are defined

A variable can have multiple references

Once one entity is referenced, no other entity can be referenced

Int main () {/ / int& eBank int a = 10; int& b = a; / / this refers to assigning the value of c to b int c = 20; b = c; return 0;} 3. Frequently referenced void TestConstRef () {const int a = 10; / / int& ra = a; / / this statement causes errors when compiling, an is a constant From const int to int const int& ra = an int& int b = 20; const int& c = b; / / ok, it can be changed from int to const int / / b, c can only read and not write b = 30 Accord / c = 30 int& the aliases of Err / / b, c can be unchanged or narrowed down / / ok / / int& e = c//err const int& e = c const int& int / int& f = 10 / there will be an error when this statement is compiled, b is the constant double& g = 10; double I = double& / double& j = h const double& j = h / h, b is the constant / b = 10 / b / b = b is the constant / Why can h be assigned to I (implicit type conversion), but not give h an alias of double type-if it's just a type problem, why just add const? / / double I = h; instead of giving h directly to I, a temporary variable (double type, constant) is generated among them, and the temporary variable is used to assign / / that is to say, const double& j = h; which means that j does not directly become an alias for h, but an alias for the temporary variable (doublde type), but this temporary variable is a constant, which explains why it is necessary to add const}

Summary

1. Can I meet the conditions for you to become an alias: you can keep or reduce your read and write permissions (const int-> const int or int-> const int), but not enlarge your read and write permissions (const int-> int).

two。 The meaning of aliases can be changed. Not every alias has the same permissions as the original name.

3. The real reason why variables of different types cannot be aliased is not that they are of different types, but that they are constant after implicit type conversion.

The meaning of frequently quoted (example stack)

Typedef struct Stack {int* a; int top; int capacity;} ST;void InitStack (ST& s) / / passes the reference in order to change the parameter to affect the argument {/ /...} void PrintStack (const ST& s) / / 1. Passing references is to reduce the number of copies by 2. At the same time, the argument will not be modified {/ /...} void Test (const int& n) / / you can receive the variable or the constant {/ /...} int main () {ST st; InitStack (st); / /. PrintStack (st); int I = 10; Test (I); Test (20); return 0;}

Summary

1. Function parameter passing if you want to reduce the number of copies using reference to pass parameters, if this parameter is not changed in the function, it is best to use const reference to pass parameters

The advantage of 2.const reference is to protect the argument from being mistakenly modified, and it can pass either ordinary objects or const objects.

4. Use scenario 1 and make parameters void Swap1 (int* p1, int* p2) {int temp = * p1; * p1 = * p2; * p2 = temp;} void Swap2 (int& rx, int& ry) {int temp = rx; rx = ry; ry = temp;} int main () {int x = 3, y = 5; Swap1 (& x, & y) / / C pass parameter Swap2 (x, y); / / C++ pass parameter return 0;}

If you want to change the shape parameter in C++, you can use pointers or references to solve the problem.

Meaning: pointer implements single-chain footer insertion | | reference implements single-chain footer insertion

Pointer

Quote void SListPushBack (SLTNode*& phead, int x) {/ / the change of phead here is the change of plist} void TestSList2 () {SLTNode* plist = NULL; SListPushBack (plist, 1); SListPushBack (plist, 2);}

Some books like to write this way (not recommended)

Typedef int SLTDataType;typedef struct SListNode {SLTDataType data; struct SListNode* next;} SLTNode, * PSLTNode;void SListPushBack (PSLTNode& phead, int x) {/ /...} 2, return value 2.1and return value return / / return value int Add (int a, int b) {int c = a + b; return Cramp / need to copy} int main () {int ret = Add (1,2) / / ok, 3 Add (3,4); cout

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