Role

xiaoxiao2021-03-05  23

As is well known, reference is made as a function parameter to avoid additional copies of parameter objects. For non-built-in types, higher efficiency can be obtained, while more secure than pointers, more clear semantics. But there is a special role in addition to this reference? Quote in the same scope, just like this:

Void f ()

{

INT i = 0;

INT & RI = I; //.

// ...

}

In fact, in the inside of F, where you need to operate i, you can use i directly, not necessarily using RI indirect operation, using i in semanticity. Mixing I and Ri are easily confused. It seems that this is a chicken rib, but it is not. Here is an example in the Qiankun Yunxun: Example, with C Macro, write code more concise this code written online program is very familiar, is the implementation of MBUF in Net / 3.

Struct MBUF

{

Struct M_HDR MHDR;

Union {

Struct

{

Struct Pkthdr MH_PKTHDR; / * M_PKTHDR SET * /

union

{

STRUCT M_EXT MH_EXT; / * M_EXT SET * /

CHAR MH_DATABUF [MHLEN];

} MH_DAT;

} MH;

Char m_databuf [mlen]; / *! m_pkther,! m_ext * /

} M_dat;

}

The above code, if I want to access the most milesty MH_DATABUF, then I must write m_dat.mh.mh_dat.mh_database; Is this very long, it is difficult to write? Such code is unknown. In fact, for MH_PKTHDR, MH_EXT, MH_DATABUF, although not in a structural hierarchy, if we stand outside MBUF, they are the properties of MBUF, which can be smashed into a plane. Therefore, there is such a set of macros in the source code:

#define m_next m_hdr.mh_next

#define m_len m_hdr.mh_len

#define m_data m_hdr.mh_data

...

#define m_pkthdr m_dat.mh.mh_pkthdr

#define m_pktdat m_dat.mh.mh_dat.mh_databuf

...

This is not very refined in this way of writing this code! Here, the macro is very cleverly solves the problem of access to deep data, but the inherent disadvantages of the macro have also been introduced into the code, and if otherwhere is unintentionally referenced by this macro defined header file, and happened to use M_Pktdat Data members, the consequences of this macro can not we want. In fact, the reference can also achieve a similar effect, but it must be used. Since the reference is not part of the standard C, this is just a C trick.

// If the code is like this:

MBUF M; // The MBUF here is the front Struct MBUF.

// If you want to use MH_EXT, you can do this:

M_EXT & MH_EXT = m.m_dat.mh.mh_dat.mh_ext;

// Then you can use MH_EXT as M.M_Dat.mh.mh_dat.mh_ext.

Maybe it doesn't look very natural, but this is undoubtedly a very straightforward approach. You can also use a const reference to logically avoid unintentional write operations. In the actual "object-oriented" C code, it is not recommended to access the direct data member. Instead, it is to use the GET () and set () methods to access data, some people use only GET, to read by returning a member. Double purpose of writing data members, this time, you can define a reference to accept the return to the return, thereby avoiding writing (xxx.get ()). Get () This drag-out statement to access a deep layer member. Another role referenced is "alias". An alias is another translation of reference, which is clearly expressed another role in reference. Just for the readability of the code: // below the code

INT i = 0, j = 0;

// ...

For (i = 0; i <10; i )

For (j = 0; j <10; j )

a [i] [j] = 0;

// Can you understand the meaning of this code? It is a bit difficult, I and J's meaning are unclear, and it is impossible to see it.

// If you change to this:

Const int width = 10;

Const int Height = 10;

// ...

INT i = 0, j = 0;

// ...

INT & line = i;

INT & ROW = J;

For (line = 0; line

For (ROW = 0; ROW

a [line] [row] = 0;

/ / Isn't it a little better? This is not a typical example, because the definition of i and j is arbitrary, some cases you must use the name given by others to be very depressed, and they must use the meaningful meaning, at this time a reference It is often a lot of refreshing.

Look at the following example:

Class CA

{

INT M_I;

PUBLIC:

INT & I;

INT const & c_i;

CA (): i (m_i), c_i (m_i) {};

}

This is a simple class that is different from the so-called "object-oriented" approach, where the public interface implements internal data is used. This method is used to correspond to another passage of the Qiankun: This is the problem of the PME model, Delphi, Java, and C # provides a syntax called attributes, probably this look:

Class A

{

Property Int X

{

Get {returnx x;}

Set {x = value;}

}

}

A a a; this can be accessed like this; int b = a.x;

This is much more intuitive than this than A.SETX (8); b = A.GETX ();

You can use CA A; A.I to access the private data member of CA to achieve the effect like attribute methods. However, this method is not satisfactory in VC6 because it stores a pointer to replace syntax, which leads to unnecessary expansion of class volume, which is what we don't want to see. Perhaps there is indeed difficulty in achieving, but it is also hoped to have a better compiler to achieve true reference interfaces.

This example is actually an attraction of the deep data member access.

Quote, as a special way of C , there may be a lot of things that are not known to wait for us to discover ~

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

New Post(0)