Why initialization list c




















Following are such cases:. In Inheritance base class constructor is called first Order of Constructor call , followed by the child class constructor. The above code example can be rewritten using initializer list, and will execute smoothly without any error. If you have a data member as reference type, you must initialize it in the initialization list. References are immutable hence they can be initialized only once. If you are assigning the values inside the body of the constructor, then a temporary object would be created which will be provided to the assignment operator.

The temporary object will be destroyed at the end of the assignment statement. Creation of temporary object can be avoided by using initializer list.

Initializing const data member const data members can be initialized only once, so they must be initialized in the initialization list. Keep Exploring. Related Courses. Learn in-demand tech skills in half the time. Early Access Courses. Assessments New. Free Trial New.

Reason for initializing the const data member in initializer list is because no memory is allocated separately for const data member, it is folded in the symbol table due to which we need to initialize it in the initializer list. See this post for a running example on this point.

Thanks to ptr for adding this point. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. A braced-init-list is not an expression and therefore has no type, e.

Also because braced-init-list has no type, special rules for overload resolution apply when it is used as an argument to an overloaded function call. Create account Log in. Namespaces Page Discussion. Views View Edit History. From cppreference. Keywords Escape sequences. Namespace declaration. Namespace aliases. Fundamental types Enumeration types Function types.

Compound types Union types. Default initialization Value initialization Zero initialization Copy initialization Direct initialization. Expressions Value categories Order of evaluation.

Operators Operator precedence. Class declaration Constructors this pointer.



0コメント

  • 1000 / 1000