[Go] C + + temporary variable life cycle

Source: Internet
Author: User
Tags getbase

Http://www.cnblogs.com/catch/p/3251937.html

Temporary variables in C + + refer to those variables that are generated by the compiler on the stack as needed, without a name.

The main uses are mainly in two categories:

1) The return value of the function, such as:

1StringProc ()23{45ReturnString"abc)  6  7 } 8  9 10 11 int main () 12 13 {14 15  proc ();  return 0;17 18}           

Where the 15th row produces a temporary variable. However, not all return values create temporary variables, and temporary variables are created only if the return value is not assigned to another variable.

This temporary variable has a short life cycle and is destroyed when the expression is completed. For example, the above code, 15 rows of temporary variables, 16 lines of time has been destroyed.

2) The intermediate variable when the type is converted.

3;  2.0;  float F = k + A;   

Line 4th, k+a because it is a float + int, int will be converted to float and then added to K, this time will produce a temporary variable. The above example is build-in type, but the same applies to custom classes.

In general, temporary variables in C + + are destroyed after the end of the expression (full expression), such as the two chestnuts mentioned above, but there are exceptions, if the temporary variable is used to initialize a reference, then the lifetime of the temporary variable will be extended until the reference is destroyed. , which does not result in dangling (dangling) references.

1StringProc ()2{3ReturnString"Abc "); } 5  6 int main ()  7  { 8 const string& Span style= "color: #0000ff;" >ref = Proc ();  9 cout << ref << Endl; return 0;11}                 

As above, the temporary variable generated by line 8th is extended until main () is returned because it has a ref point.

This feature is sometimes useful, for example, you can point to a subclass's temporary variable with a reference to a base class, and then use that reference to implement polymorphism without having to deal with the destruction of subclasses.

1Class Base ()2{3Public:45Virtual Bar () {cout <<"Base Bar ()"<<Endl }6};78 Class Derone:PublicBase9{10Public:1112Virtual Bar () {cout <<"Derone Bar ()"<<Endl }13};1415Class Dertwo:PublicBase16{17Public:1819Virtual Bar () {cout <<"Dertwo Bar ()"<<Endl }20};21st2223Base getbase ()24{25ReturnBase ();26}2728Derone Getderone ()29{30ReturnDerone ();31}3233Dertwo Getdertwo ()34{35ReturnDertwo ();36}373839IntMain ()40 {41 Const base& REF1 = GetBase ();  Getderone ()  Getdertwo () 45  Ref1. Bar (); 46  Ref2. Bar (); 47  REF3. Bar (); 48 49 return 0;                 

This trick is used in the implementation of Loki::scopeguard, so that when using polymorphism within a domain, you can avoid using pointers, which is so ingenious that the Andrei is called: "the most Important const". However, it is important to note that temporary variables can only be pointed to by a const reference and are therefore non-modifiable.

[Go] C + + temporary variable life cycle

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.