References in C + + __c++

Source: Internet
Author: User

The concepts of references and pointers in C + + are often compared, and there are very similar characteristics between them. This brings trouble to beginners, especially when many compilers use pointers to implement references, adding to the confusing relationship between the two. This article provides a brief introduction to the characteristics of the reference, designed to help readers better understand the concepts of references and avoid common errors. three major attributes of a reference no null reference exists all references are initialized the object that the reference points to cannot be changed

By carefully appreciating the three characteristics of the above references, you will find that these three points are relative to the pointer. With respect to these three points, the pointer has the exact opposite character. So the above three points in fact is also a reference and pointers of the three major differences. Readers should be careful and bear in mind that most of the misuse of citations breeds and misunderstandings about the three major differences. the nature of the reference is alias

The three major characteristics of the reference, on the surface appear to be relative to the characteristics of the pointer summed up, and the difference! But

The nature of the reference is the alias

It is the intrinsic source of these three characteristics, in other words, the object to which the reference is directed must be created before the reference. This is the same as using a typedef to create a type alias.

template< TypeName T1, TypeName t2>
class MyType
{public
   :
   MyType ();
   MyType (const mytype&);  
   void swap (t1&, t2&);
};

int main ()
{
   typedef mytype<int, short> ttype;

   Ttype Swapper;
   int a = ten;
   Short B = 2;
   Swapper.swap (A, b);

   return 0;
}
common errors referred to
double& d = 10.5; Error

Double A = 10.5;
double& d = A;    That's right

Where is the mistake? A more in-depth understanding of this involves the concepts of left, right, left, and right references. To clarify the problem as soon as possible, we make a brief introduction to the concept of the left value and the right value.

The left value is the persistent object that persists after the expression ends
The right value corresponds to the temporary object, which does not exist at the end of the expression

In an assignment expression, the left side of the equal sign is generally the left value, and the right is generally the right value. So there is the name of the left value right, but the judgment of the left value should not be used as standard, but should be defined.

D is a left-value reference in the above code, and 10.5 is obviously a temporary object and therefore a right value. Here's the problem, a very important left-value reference can be bound to a right value object. The answer is no, we can get the answer according to C + + standard:

The C + + standard stipulates that a very much left-value reference can only be bound to a very left value

So d = A is the correct usage in the above code. So why is the standard so prescribed? Imagine a function:

void swap (double& d1, double& D2);
...
Swap (10.0, 1.0);

This code is not compiled because obviously he violates the specification. The swap function here will obviously modify its parameters, and if we allow the temporary variable to be entered as a parameter, we cannot get the modified result. So the input parameter must be an argument that has been created and initialized so that we can get the result of the function modification. Here's a summary:

When a function's argument is a very variable reference, the programmer is actually implying that the object The compiler refers to will be modified by the function, so the caller must ensure that the argument entered is a left value (not a temporary variable). Otherwise, the changes made by the function will not be available. In order to avoid the danger and uncertainty caused by this, C + + stipulates that in this case, the temporary variable is not assignable to the extraordinary reference.

What objects can be bound to a constant left value reference ? Ah, it is almost unstoppable, can be tied up to see anyone.

Double d = 2.1;
Const DOUBLE  e  = 0.2;
Const double& D1 = d + E;
Const double& d2 = 1.4;
Const double& D3 = D;
Const double& D3 = e;

He's completely fine in these situations. So the following code is OK:

Const std::string& s = std::string ("Hello") + "world!";

Well, what about this one?

std::string& s = std::string ("Hello") + "world!";
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.