Constructor initialization list

Source: Internet
Author: User

The constructor initialization list is sometimes necessary. Although the constructor is divided into initialization and computing stages, it is more efficient to use the initialization constructor list. This is one of them and must be used in some cases. The following is an example,

1 class Foo 2 {3 Public: 4 Foo (int x, int y): A (x), B (y) {} 5 6 private: 7 int; 8 int B; 9}; 10 11 class bar12 {13 public: 14 bar (): Foo (10, 10) {} 15 private: 16 Foo; 17 };

The type of member Foo does not have a default constructor. Calling the default constructor in the initialization phase will fail. If there is no bar constructor initialization list.

Therefore, you must use the initialization list for any const, reference type members, and members without the default constructor class type.

Constructor initialization list

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.