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

Case Analysis of C++ quotation and inline function

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "C++ citation and inline function example analysis". In daily operation, I believe many people have doubts about C++ citation and inline function example analysis. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "C++ citation and inline function example analysis". Next, please follow the editor to study!

Quote the initial stage

Reference is one of the features of C++, but C++ does not specifically give the reference a keyword, using operator overloading. Citation is common in C++, which means it is important. Let's talk about citation in two levels, and the first stage is what we can see in books.

What is a citation?

A reference is not a new definition of a variable, but an alias to an existing variable. The compiler does not open up memory space for the reference variable, which shares the same memory space as the variable it references. The so-called nickname is a nickname. Zhang San may be called Sanzi in front of his friends and Saner in front of his elders, but whether they are called Sanzi or Saner, it is undeniable that they are called Zhang San. The referenced symbol is the same as the & operator we took the address, which we will see later is why the operator is overloaded.

We can understand the so-called quotation.

# include int main () {int a = 10; int& b = a; / / b is an alias of a printf ("% p\ n", & a); printf ("% p\ n", & b); return 0;}

Why should there be a citation?

References have many advantages, one of which is that the code can be simplified. We wrote in C language how to exchange two integer data with the help of one-dimensional pointers, but this is a little troublesome. Using references can solve this problem very well.

# include void swap (int& a, int& b) {int c = a; a = b; b = c;} int main () {int a = 10; int b = 20; printf ("before exchange a =% d b =% d\ n", a, b); swap (a, b); printf ("after exchange a =% d b =% d\ n", a, b) Return 0;}

The reference points to the same space.

No matter how many nicknames we give to the variable, but these nicknames point to the same space, as long as we change the space of one nickname, it will make the other nicknames worth changing.

# include using namespace std;int main () {int a = 10; int& b = a; / / b is an alias for an int& c = b; / / you can also alias c to return 0;}

Int main () {int a = 10; int& b = a; / / b is an alias for an int& c = b; / / you can also alias c = 20; 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