| |

VerySource

 Forgot password?
 Register
Search
View: 1447|Reply: 13

About pointers and references

[Copy link]

1

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-19 09:20:02
| Show all posts |Read mode
Excuse me
When passing parameters to functions, when are pointers or references passed? What are their advantages?
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-26 00:18:01
| Show all posts
The pointer is more flexible and can adapt to many situations
References seem more convenient, but they are not absolutely safe, such as:

int * pt = 0;
int&ref = * pt; // This reference is the same as a null pointer
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Hong Kong

Post time: 2020-1-26 01:27:02
| Show all posts
Many books have covered this issue.
A reference is also a pointer, but it is a pointer that does specify the address. Therefore, you do not need to consider whether the reference is null when passing the reference, and the pointer must be considered, and the transfer method is adopted according to the actual situation.
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 Unknown

Post time: 2020-1-26 03:27:01
| Show all posts
Indifferent advantages,
It ’s just a matter of habit ~~

There are no references in C, only pointers;
C ++ is expected to use more references ~~
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-26 03:45:01
| Show all posts
When to use pointers and when to use references, there is no standard to follow
If necessary, if you want to point to other objects after passing in, or if you want to traverse in the array, you can only use pointers.

My own policy is to "use references as much as possible" but not to exclude pointers
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-26 07:45:02
| Show all posts
Where you can quote, try to quote
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-26 07:54:01
| Show all posts
goodjianli: If you look at my example, you will not say that there will be no empty references

There is also a case, if I declare a reference, but do not point to anyone, it is as serious as a null pointer and a wild pointer
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-26 20:36:01
| Show all posts
Upstairs, references must be assigned initial values
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-26 22:45:01
| Show all posts
First, realize that you cannot use a reference to a null value under any circumstances. A reference must always point to some object. So if you use a variable and let it point to an object, but the variable may not point to any object at some point, you should declare the variable as a pointer, because then you can assign a null value to the variable. Conversely, if the variable must point to an object, such as your design does not allow the variable to be empty, then you can declare the variable as a reference.
 "But wait a minute," you ask skeptically, "What are the consequences of such code?"
char * pc = 0; // set pointer to null
char&rc = * pc; // make the reference point to a null value
This is very harmful, no doubt. The result will be indeterminate (the compiler can produce some output, causing anything to happen). People who write such code should be avoided unless they agree to correct the mistake. If you are concerned that such code will appear in your software, then you'd better avoid using references altogether, or else let better programmers do it. We will ignore the possibility of a reference pointing to a null value later.
The above quoted from More Effective C ++ Article 1
Reply

Use magic Report

0

Threads

23

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-31 23:09:02
| Show all posts
In the C ++ standard, it is indeed forbidden to not initialize the reference definition. The problem is, if some people do not know this, and
When experimenting in an old compiler, uninitialized references will not cause compilation errors, it may just be a warning, or even
In some special compilers, uninitialized references are defined as something like a null pointer. In some existing enterprise-level
The code even takes advantage of a feature that is valuable in the context of some applications. -Such a system,
Of course, you can change all to pointers. The problem is that it takes time, many, many hours. In this way, the company would rather use
The old antique compiler made some simple modifications to the original code, rather than rewriting it completely, because
The price is too high.

There is a gap between the standard and the implementation. It can only be promoted as much as possible, but sometimes don't kill yourself with a lot of original code.
The C ++ compiler that needs to be maintained and absolutely conforms to the standard. As far as I know, it should not be available at present, even the academic compiler,
Can only do 99% or 99.9%
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list