当前位置:网站首页>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
边栏推荐
- Gensim error attributeerror: type object 'word2vec' has no attribute 'load_ word2vec_ format‘
- 给网站添加SSL安全证书
- [untitled] fish pond forwarding command
- 技术分享 | MySQL中一个聚类增量统计 SQL 的需求
- Redis学习(1)——概述和常用命令
- Taobao secsha plug-in
- Atom的一些操作
- Imitation B station web, app, background
- Using the path module to prompt errors in ts+vite projects
- How is the redemption time of financial products stipulated?
猜你喜欢

. bash_ profile

Installation of oracle19c under alicloud lightweight application server linux-centos7

Chapter 3 - data link layer

Route add add route

Teach you to stop visiting a website

Vscade, open a folder or workspace... (file - > open folder) solution

How does JMeter implement interface association?

Mqtt keepalive and reconnect

Disputes between chromedriver, geckodriver, microsoftwebdriver, iedriverserver and operadriver

Alibaba cloud link tracking is on the Net project (Jaeger trace)
随机推荐
C2 hiding of traffic encryption
7hutool actual fileutil file tool class (common operation methods for more than 100 files)
Imitation B station web, app, background
Redis学习(3)—— 持久化机制
ARP interaction process
Tcp/ip FAQs
How does JMeter implement interface association?
Chapter 3 - data link layer
Summary of the most basic methods of numpy
Installation and application of MySQL 8.0 under Linux
Reptile Foundation_ Requests Library
A blazor webassembly application that can automatically generate page components based on objects or types
Async get and post request interface data (add, delete, modify and query pages)
Write a code hot deployment
Gensim error attributeerror: type object 'word2vec' has no attribute 'load_ word2vec_ format‘
Two of my essays
2021 the latest selenium truly bypasses webdriver detection
流量加密之C2隐藏
Write a compile time annotation
. bash_ profile