C ++ learning by referencing variables and learning by referencing variables

Source: Internet
Author: User

C ++ learning by referencing variables and learning by referencing variables

(1) reference variable (rv) is mainly used as a form parameter of the equation. rv can be used to directly operate the original data instead of copying the data, saving time and space, in particular, for a struct and a class object, it is a good choice to pass the value of common data.

(2) define a reference variable in the form of type & name. refer to the example in c ++ premier plus,

Int rats;

Int & rodents = rats; (int & means "reference to an int variable ")

A rodents is defined. It refers to the reference variable of rat. After initialization, rats and rodents have the same value and point to the same place, which can be used interchangeably, it is "one thing, two names ".

(3) The rv must be initialized during the Declaration. After initialization, the pointing point will not change, which is equivalent to a const type pointer. Subsequent assignments can only change the value but cannot change the address. For example

Int & rodents = rats; equivalent

Int * const pr = & rats;

(4) When the formal argument of a function is an rv pointing to the const type, the compiler will generate temporary variables in the following two cases,

  • When the real parameter type is correct, but it is not "Left value (lvalue )"
  • If the real parameter type is incorrect, it can be converted to the correct type.
  • (5) The C ++ 11 standard introduces a new syntax called "rvalue reference". Therefore, the original reference is now called "lvalue reference )"

    (6)What is the difference between the two methods of function return reference and return value?

    For the return method of "return value", the object after the return statement is copied to a temporary memory, then, the data in the temporary memory is copied to the corresponding object of the called function, but if it is "return reference", the object after the return statement is directly copied to the corresponding object of the called function, faster.

    (7) What is the purpose of returning a const-type reference?

    The const type can be used to return a reference to avoid modifying the return value.

    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.