C + + Basics

Source: Internet
Author: User

Name space

Mainly to solve the problem of name conflict

Reference

Aliases for variables

The difference between a reference and a pointer:

1. The pointer is a variable, you can assign it to another address

2. The reference must be initialized at the time of creation and will never be associated with a different variable. The pointer can be assigned to null at the beginning and can then be changed to point elsewhere.

Since pointers are also variables, there can be references to pointer variables, such as the following code that looks a bit weird

int* a = NULL;     int*& RA = A; // The reference Ra that represents int* is initialized to a    int 9 ;     = &b; // Ok,ra is an alias of A and is a pointer
    • Void references are not legal

void& A = 3;void is only syntactically equivalent to a type, not essentially a type, without any one variable or the type of reference is void

    • Cannot create a referenced array
int a[] ={0}; int& ra[] = A; // Error
    • No references to pointers and references
int A; int& ra = A; int& *p = &ra; // error start defines a pointer to a reference
    • There is a null pointer, but there is no null reference
Parameter passing by reference
    • Passing a reference to a function is the same as passing a pointer. Can change the value of a variable.
    • Using a reference as a parameter has a clearer syntax than using pointers
    • Use references as parameters \ Return values to give meaning to a function

1. function can only return one value, what if you want to return two values? You can pass two arguments to a function by reference, and then the function fills the target with the correct value

2. Returns a copy of a value when the function returns a value and does not require a copy to be returned with a reference, resulting in increased efficiency

Note: If you return a variable or reference that is not scoped, there is a problem. This is as serious as returning a local-scope pointer, such as

C + + Basics

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.