constructor problems encountered in inheritance in C + +

Source: Internet
Author: User

Today, at development, I encountered a constructor problem that I had always taken for granted.first to summarize:when a subclass is constructed, the parent class's default constructor (parameterless) is called first if the constructor of the parent class is not explicitly called .
Here are some examples of different situations Example One: The parent class has a default constructor, and the constructor of the subclass is arbitrarily
#include <iostream>class base{public:base () {}};class derive:public base{public:derive (int a,int b) {}};int main ( {Derive c); return 0;}

Result: compilation passed, temporarily does not explain the problem.
Example Two: The parent class does not have a default constructor, and the constructor of the subclass is arbitrarily
#include <iostream>class base{public:base (int a) {}};class derive:public base{public:derive (int a,int b) {}};int Main () {Derive C; return 0;}

Result: Compile error, error message: [ERROR] no matching function for calls to ' base::base () ' Description, subclass construction requires calling the parent class's default constructor first
example Three: The parent class does not have a default constructor, the constructor of the subclass is random, but the constructor of the parent class is explicitly called
#include <iostream>class base{public:base (int a) {}};class derive:public base{public:derive (int a,int b): Base (a) {}};int main () {Derive C (); return 0;}

Result: compilation succeeded in stating that the default constructor for the parent class is no longer called after the constructor of the parent class is explicitly called
example four: The parent class does not have a default constructor, and the subclass's constructor is the same as the parent class's argument list, but does not explicitly call the parent class's constructor
#include <iostream>class base{public:base (int a) {}};class derive:public base{public:derive (int a) {}};int main () {Derive C (1); return 0;}

Result: Compilation error description, in fact, this situation is the same as the case of the example two, except that the parent class is the same as the constructor parameters of the subclass, so it will give the sense that the subclass will call the parent class constructor of the same parameters, but in fact, the child class is called the default constructor of the parent class first. Because the parent class does not have a default constructor, the error message is the same as the example two.
Example Five (TRAP): The parent class has the same parameters as the child class's constructor, and the parent class has a default constructor. The subclass does not explicitly call the constructor of the parent class.
#include <iostream>class base{public:base (int a) {this->a = A;} Base () {}public:int A;}; Class Derive:public base{public:derive (int a) {}};int main () {Derive C (1); Std::cout << c.a << Std::endl; return 0;}

Result: Compile successfully, run output result is: 3674912 description, this situation is the same as example four, only because the parent class has a default constructor, so it will compile successfully. The constructor of the subclass with an int argument does not call the parent class with an int argument constructor.
Finally, one more summary: The above five examples show that the default constructor of the parent class is called by default if the constructor for the parent class is not explicitly called in the constructor of the subclass. In other words, when using inheritance, if the parent class does not have a default constructor, the child class's constructor should explicitly call the parent class's custom constructor.

constructor problems encountered in inheritance in C + +

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.