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 is the basic concept cited by C++?

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

Share

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

This article to share with you is about the basic concept of C++ reference is what, Xiaobian think quite practical, so share to everyone to learn, I hope you can read this article after some harvest, not much to say, follow Xiaobian to see it.

C++ programming language has many applications in common with C programming language. As an upgraded version of C, what are the differences? Let's start with the basic concepts referenced in C++ to interpret this problem, hoping to help you easily grasp the characteristics of this language.

The concept of C++ references

A reference introduces a synonym for an object. Definition references are represented in a similar way to definition pointers, except that & is used instead of *.

Point pt1(10, 10);

Point &pt2= pt1; defines pt2 as a reference to pt1. With this definition, pt1 and pt2 represent the same object.

It is important to emphasize that C++ references do not produce copies of objects, but are simply synonyms for objects. Therefore, when the following statement is executed:

pt1.offset(2,2);

Both pt1 and pt2 have values of (12, 12).

A C++ reference must be initialized immediately upon definition because it must be synonymous with something. You cannot define a reference before initializing it. For example, the following statements are illegal:

Point &pt3; pt3=pt1;

So since a quote is just a synonym for something, what is its use?

The two main uses of C++ references are discussed below: as arguments to functions and to return lvalues from functions.

II. Reference Parameters

Transfer variable parameters

In traditional c, parameters are passed by value when the function is called, which means that the parameters of the function do not have the ability to return a value.

So in traditional c, if you need a function parameter with the ability to return a value, it is often implemented through a pointer. For example, realize

The c program for exchanging values of two integer variables is as follows:

void swapint(int *a,int *b) { int temp; temp=*a; *a=*b; *b=temp; }

After using the C++ reference mechanism, the C++ version of the above program is:

void swapint(int &a,int &b) { int temp; temp=a; a=b; b=temp; }

The C++ method that calls this function is swapint (x,y); C++ automatically passes the address of x,y as an argument to the swapint function.

The above is what the basic concept of C++ reference is, Xiaobian believes that some knowledge points may be what we see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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