C Syntax error: The base class constructor call is incorrect, how to deal with it?
In C programming, we often encounter situations where base class constructors are called. However, during this process, sometimes the base class constructor is called incorrectly. This situation often results in the program exiting abnormally or causing an unknown error. If you encounter this situation, don't panic. This article will give you a detailed introduction to the incorrect base class constructor call and how to deal with it.
1. Incorrect calling of base class constructor
In C, the constructor of a derived class must call the constructor of its base class to ensure that all members of the base class are Initialized correctly. Generally speaking, it is the most common method to call the base class constructor in the member initialization list of the derived class constructor. However, when you make a mistake in calling a base class constructor, there will be situations where the base class constructor is called incorrectly. Listed below are several common situations in which base class constructor calls are incorrect:
#include<iostream> using namespace std; class Base{ public: Base(){} Base(int a){ cout<<"Base class with value : "<<a<<" ";} }; class Derived: public Base{ public: Derived(){} Derived(int a){ cout<<"Derived class with value : "<<a<<" ";} }; int main(){ Derived d(10); // 编译错误:没有与此调用匹配的函数 return 0; }
#include<iostream> using namespace std; class Base{ public: Base(){ cout<<"Base class constructor called "; } }; class Derived: public Base{ public: Derived(){ cout<<"Derived class constructor called "; } Derived(int a){ cout<<"Derived class constructor with value : "<<a<<" called "; } }; int main(){ Derived d; return 0; }
The output result is:
Base class constructor called Derived class constructor called
In the above code, the constructor of the Derived class calls the constructor of the Base class, so "Base class constructor called" is output. However, since the Derived class has only one constructor, the parameterless constructor is called by default, so "Derived class constructor called" is also output. If you call the base class constructor twice, you will get an error:
#include<iostream> using namespace std; class Base{ public: Base(){ cout<<"Base class constructor called "; } }; class Derived: public Base{ public: Derived(){ cout<<"Derived class constructor called "; } Derived(int a){ cout<<"Derived class constructor with value : "<<a<<" called "; } }; int main(){ Derived d(10); return 0; }
The output result is:
Base class constructor called Derived class constructor with value : 10 called Base class constructor called
Because the Base class is called twice in the constructor of the Derived class constructor, so "Base class constructor called" is output twice. This is because in C, the construction process of a derived class object first calls the base class constructor and then the derived class constructor. Therefore, if you call the base class constructor twice in the derived class constructor, the base class constructor will be called twice, causing an error.
#include<iostream> using namespace std; class Base{ public: Base(){ f(); } virtual void f(){ cout<<"Base "; } }; class Derived: public Base{ public: Derived(){ cout<<"Derived "; } void f(){ cout<<"Derived "; } }; int main(){ Derived d; return 0; }
The output result is:
Derived
In the above program, the f() function in the base class constructor is a virtual function, and when the Derived object is created, the derived class The constructor first calls the constructor of the base class, so the f() function of the Base class is called. However, when f() is called in the base class constructor, the constructor of the derived class object has not yet been executed, so the f() function in the derived class has not yet been called, only the f() function of the base class has been called. Therefore, the output is "Base" instead of "Derived".
2. How to deal with the incorrect call of the base class constructor?
If you encounter a situation where the base class constructor is incorrectly called, how should you deal with it? Listed below are several ways to handle incorrect base class constructor calls:
In short, when you encounter an incorrect call to the base class constructor, don't panic. You should carefully check the error and handle it according to the above processing methods. In this way, you can effectively avoid program running errors caused by incorrect calls to the base class constructor.
The above is the detailed content of C++ syntax error: The base class constructor call is incorrect, how to deal with it?. For more information, please follow other related articles on the PHP Chinese website!