Some explanations about const reference

xiaoxiao2021-03-06  75

A few days ago

, Encounter a problem in the program

When I checked the program error

In the STL implementation, I found the following code.

:

#include

Using Namespace STD

;

Class A

{

public

:

A

(

Const string

& S

) {

cout

<<

.c_str

() << Endl

;}};

int

main

() {

Const

charr

* PSZ

=

"ABC"

;

A a a

(PSZ

);

Return

0

}

Here

I feel very strange

, Because I have never done this

How can I point a reference to a type different from the reference itself?

?then

, I sent a post online.

,quickly

Someone extracted the following from the CPL 3RD as a reply

:

// BeginInitialization of a reference is trivial when the initializer is an

Lvalue

(An Object Whose Address You Can Take

See §

4.9.6

). The

Initializer

For a

'Plain' T

& Must be an Lvalue of Type T

. The

Initializer

For a

Const T

& Need

NOT BE An Lvalue

OR EVEN OF TYPE T

.

In Such Cases

(David

'' s Notes

:

And Only in Such Cases

), [

1

] First

, Implicit Type Conversion To T IS Applie

IF Necessary

(See §C

.6

[

2

] THEN

, Resulting Value Is Placed in a Temporary Variable of Type T

;

and

[

3

] Finally

,

This Temporary Variable is buy as the value of the initializer

.

Consider

:

Double

& DR

=

1

;

// error: LValue Needed

Const

Double

& CDR

=

1

;

// Okthe Interpretation of

THIS Last Initialization Might BE

:

Double Temp

=

Double

(

1

);

// first create a Temporary with the right value

Const

Double

& CDR

= TEMP

;

// then use the temporary as the initializer for cdra temporary created to hold a reason

The end of its reference's scope

.

References to variables

And References to Constants Are Distinguished

Because the Introduction of a Temporary in the

Case of the variable

IS Highly ErrorProne

;

An Assignment to the Variable Would Become An Assignment To The - Soon to Disappear - Temporary

No Such Problem EXISTS

For References

To constants

,

And References to Constants Are Offen Important AS

Function Arguments

11.6

).

// end

Means of

, As long as there is a feasible conversion path

The compiler will create a temporary variable as the reference type type information when constructing.

,then

, Point to this temporary variable with reference

.at the same time

The above text also gave us the following information.

:

1. Non

Const Reference will not enjoy such special treatments

The following code is wrong

:

Float F

=

0

;

Double

& D

= F

;

charr

* PSZ

=

"ABC"

;

String

& RSTR

= PSZ

;(Note

: VC6 reports an incorrect message with actual inconsistency when compiling each other

: A Reference That IS

Not to

'Const' Cannot Be Bound To a Non

-LValue

Here, F and PSZ are clearly left value

)

2.

Const Reference will make the function more applicable

But the price may introduce a large overhead when performing type conversion

, Lose the efficiency of the reference transfer

.

of course

In addition to this

,

Const is just a compile time concept

,with

public

/

protected

/

Private, etc.

, Just tell the compiler

We need to add some additional descriptions to things after these indications.

Please distinguish between treatments

These descriptions

PRIVATE

,says

: If you find non-book or

Friend visited these members

(method

), Report errors

;for

CONST

It is said that

: If you find any action pairs when you have a syntax check?

CONST modified objects have been modified

, Report error

.

After the syntax check

These modifications should be quietly retired.

.

转载请注明原文地址:https://www.9cbs.com/read-93331.html

New Post(0)