The difference between C + + references and pointers

Source: Internet
Author: User
Tags shallow copy

I. Definitions of references and pointers

Reference: It takes an alias to another variable and does not allocate space again (which can lead to optimization of the program)

Pointer: It is an entity that needs to allocate space

references must be initialized at the time of definition, and space cannot be changed .

The pointer does not have to be initialized at the time it is defined, and the space it points to is variable . (Note: The referenced value cannot be null)----------> can bring program security

A reference to access a variable is directly accessible, while a pointer to access a variable is an indirect access

Second, C + + initialization function 6 default functions

1. Constructors

2. Destructors

3. Copy constructors (when function arguments are objects)-------> shallow copy deep copy

Text T1;

Text T2 (T1)

Text t2 = t1;

4. Assignment function-----------> Shallow Assignment depth value

Text T1;

Text T2;

t2 = T1;

5.Text A;

Text *b = &a;

text* operator& () {return this};

  

6.const Text A (constant object);

Const Text *B = &a;

Const text* operator () {return this};

Third, deep copy, shallow copy

Execution procedure: Call the constructor once, customize the copy constructor once, and two times the destructor. A pointer member of two objects refers to a different memory.

A shallow copy is just a copy of the pointer, and the two pointers point to the same memory space

A deep copy not only copies the pointer, but also copies the contents of the pointer, which is a pointer to two different addresses.

Student::student () {      name = new char ();      cout << "Student" << Endl;} Student::~student () {         cout << "~student" << (int) name << Endl;         Delete name;         name = NULL;} Student::student (const Student &s) {         name = new char (a);         memcpy (name, S.name, strlen (s.name));         cout << "Copy Student" << Endl;}

The difference between C + + references and pointers

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.