What are the cases in C + + that can only be initialized with a list and not assigned?

Source: Internet
Author: User

Original link: http://blog.csdn.net/yuliu0552/article/details/6631509

What are the cases in C + + that can only be initialized with a list and not assigned?

(1) For const and reference type member variables, they can only be initialized and cannot be assigned, so they can only use the initialization list;

Initialization is different from assignment:
Assignment is to delete the original value, give the new value, the constructor means to open space and then assign a value, can only be assigned, not initialized;

Initialization list is not the same, open space and initialization is done at the same time, directly give a value;

Constants cannot be assigned and can only be initialized, so they must be completed in the initialization list;

The C + + reference must also be initialized, so it has to be done in the initialization list.

1) references must be initialized, pointers unnecessary;

2) The reference initialization cannot be changed, and the pointer can change the object being referred to;

3) There is no reference to a null value, but there is a pointer to a null value.

In addition, when a subclass initializes itself, if the parent does not have a default constructor, it must also be initialized with the initial session list, and the constructor of the parent class is called in the list table

What are the cases in C + + that can only be initialized with a list and not assigned?

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.