Problems with the C + + default constructor

Source: Internet
Author: User

C + + Defaul construct: default constructor (default constructor)

Definition: The first constructor has no parameters, that is, a () Form

All parameters of the second constructor are provided by default values, A (int a=0,int b=0)

The compiler adds a condition for the default constructor: If you create a class you do not write any constructors, the system automatically generates the default parameterless constructor, the function is empty, nothing is done (this constructor is only a case where the trival

Relationships of derived classes and base classes:

We typically say derived classes and base classes, when we call a custom constructor for a derived class, the derived class automatically calls the default construct function in the base class, and cannot call the other constructors in the base class

classFoo {Private:   intVal; Public: Foo (): Val (9){}}; classBar: PublicFoo { Public:   Char*str; inti; Bar (intIChar*5) {i=i; STR=s; }}; //cannot pass because the bar derived class cannot call the Defalut function in the base class because there is noclassFoo {Private:   intVal; Foo (inti): Val (i) {}}; classBar: PublicFoo { Public:   Char*str; inti; Bar (intIChar*s) {i=i; STR=s; }};
classFoo {Private:   intVal; Public: Foo (inti): Val (i) {} Foo (): Val (6){}}; classBar: PublicFoo { Public:   Char*str; inti; Bar (intIChar*1) {i=i; STR=s; }}; When you define both the normal constructor and the default construct function in base, the derived class can invoke the

Problems with the C + + default constructor

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.