当前位置:网站首页>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 6

The 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

原网站

版权声明
本文为[Modest learning and progress]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202221424252241.html