当前位置:网站首页>Interview - difference between pointer and reference
Interview - difference between pointer and reference
2022-06-21 14:31:00 【Modest learning and progress】
One 、 The definition and nature of pointer and reference :
(1) The pointer : The pointer is a variable , It's just that this variable stores an address , Point to a storage location in memory , namely A pointer is an entity ; and quote It's essentially the same thing as the original variable , It's just one of the original variables Alias nothing more . Such as :
int a=1;int *p=&a;
int a=1;int &b=a;It defines an integer variable and a pointer variable p, The pointer variable points to a The storage unit of , namely p The value of is a Address of the storage unit .
And below 2 Sentence defines an integer variable a And this plastic surgery a References to b, in fact a and b It's the same thing , Occupy the same storage unit in memory .
(2) Sure Yes const The pointer , however No, const quote ;
(3) Pointers can have multiple levels , however References can only be one level (int **p; legal and int &&a It's illegal )
(4) The value of the pointer can be null , however quote Value Not for NULL, And the reference must be initialized when it is defined ;
(5) The pointer After initialization You can change , That is to point to other storage units , and quote It won't change after initialization , be faithful to one 's husband unto death .
(6)”sizeof quote ” What you get is the variable you're pointing to ( object ) Size , and ”sizeof The pointer ” What we get is the size of the pointer itself ;
(7) Autoincrement of pointers and references (++) The meaning of operation is different ;
Two 、 The same thing
It's all about the concept of address ;
Pointer to a block of memory , Its content is the address of the memory ;
A reference is an alias for a block of memory .
3、 ... and 、 contact
1、 References are implemented with pointers inside the language ( How to achieve ?).
2、 For general applications , Understand a reference as a pointer , No serious semantic mistakes
static Singleton* GetInstance()
{
static Singleton instance; // Local static objects
return &instance; // The pointer
}
Singleton* s1 = Singleton::GetInstance();.
A reference is a pointer whose operation is restricted ( Only content fetching operation is allowed ).
The quotation is C++ The concept of , Beginners tend to confuse references with pointers . In the following procedure ,n yes m A reference to (reference),m It's a citation (referent).
int m;
int &n = m;n amount to m Another name for ( nickname ), Yes n Any operation of is to m The operation of .
Some of the rules cited are as follows :
(1) References must be initialized at the same time as they are created ( Pointers can be initialized at any time ).
(2) Can not have NULL quote , The reference must be associated with a legal storage unit ( The pointer can be NULL).
(3) Once the reference is initialized , You can't change the citation relationship ( The pointer can change the object at any time ).In the following example program ,k Is initialized to i References to . sentence k = j It's a k The value of is changed to 6, because k yes i References to , therefore i The value of 6.
int i = 5;
int j = 6;
int &k = i;
k = j; // k and i All of the values become 6The above program looks like playing a word game , It doesn't reflect the value of quotation . The main function of reference is to transfer the parameters and return values of functions .C++ In language , There are three ways to pass function parameters and return values : Value passed 、 Pointer passing and reference passing .
“ reference ” Property image of “ Pointer passing ”, And writing is like “ Value passed ”. actually “ quote ” Anything you can do “ The pointer ” Can also do , Why “ quote ” This thing ?
The answer is “ Do the right job with the right tools ”.
The pointer can manipulate what's in memory without restriction , Although the pointer is powerful , But it's very dangerous .
It's like a knife , It can be used to cut down trees 、 cut paper 、 manicure 、 Haircut and so on , Who dares to use ?
If you really just need to borrow from an object “ Alias ”, Then use “ quote ”, And don't use “ The pointer ”, To avoid accidents . for instance , Someone needs a certificate , It would have been OK to put the official seal on the document , If you give him the key to take the official seal , Then he gets the right he shouldn't have .
in general , You should use a pointer when :
One is that you consider the possibility of not pointing to any object ( under these circumstances , You can set the pointer to null ),
Second, you need to be able to point to different objects at different times ( under these circumstances , You can change the direction of the pointer ). If you always point to an object and once you point to an object, you don't change the direction , So you should use references .
There's another situation , When you overload an operator , You should use references .
Use references whenever possible , Use the pointer when you have to .
When you Unwanted “ Point back to ” when , References generally take precedence over pointers . This usually means It is more useful when referring to the public interface used for the class . The typical case of reference is the surface of the object , The pointer is used inside the object .
http://www.cnblogs.com/tracylee/archive/2012/12/04/2801519.html
http://www.cnblogs.com/dolphin0520/archive/2011/04/03/2004869.html
边栏推荐
- Compile time annotation automatically generates dataholder
- MySQL failover and master-slave switchover based on MHA
- What fun things can a desk service do
- Explain the high availability features of Nacos
- 2021 the latest selenium truly bypasses webdriver detection
- 网上开户安全吗?新手可以开账户吗
- Win10 installation and configuration mongodb
- Qt-2-signal and slot
- JS written test question: array
- Is it safe to open an account online? Can a novice open an account
猜你喜欢
![[Goo Goo donkey takeout rebate system] customer service configuration tutorial of takeout CPS project - (attached with picture and text building tutorial)](/img/54/d30b9c08e46760dac8c3405716941f.jpg)
[Goo Goo donkey takeout rebate system] customer service configuration tutorial of takeout CPS project - (attached with picture and text building tutorial)

Chapter 2 - physical layer (I)

Viewing tcp/ip network communication from the sending of an email

7hutool actual fileutil file tool class (common operation methods for more than 100 files)

Summary of the most basic methods of numpy

What are the log files in MySQL?

Design and implementation of object system in redis

USB message capture tcpdump

Mqtt keepalive and reconnect

Async get and post request interface data (add, delete, modify and query pages)
随机推荐
Use ant for running program with command line arguments
How to unit test non pure functions -- unittest
Qt-5-multi window programming
Chart. JS 2.0 doughnut tooltip percentage - chart js 2.0 doughnut tooltip percentages
Three questions for learning new things
The whole process of Netease cloud music API installation and deployment [details of local running projects and remote deployment]
Numpy: basic package for high performance scientific computing & data analysis
Detailed explanation of hashtable source code in C #
网上开户安全吗?新手可以开账户吗
理财产品的赎回时间是怎么规定的?
Usage of SED (replacement, deletion of text content, etc.)
Continuous attack animation
Format printout
C#&. Net to implement a distributed event bus from 0 (1)
How is the network connected
Pyqt5 learning notes of orange_ Basic structure of pyqt5 GUI program
Small case of throttling function
Compile time annotation
Reptile Foundation_ Requests Library
Num in tensorflow basiclstmcell_ What are units- What is num_ units in tensorflow BasicLSTMCell?