C ++ member function inherited by default (two) --- succession

Following the default class member functions Cheng Zhongji

In succession, the same member functions Default Default base class member function and the general class. There are six default member function.

Derived class member function defaults

1. Constructor

Derived class constructor must complete the construction of the base class , if not the base class constructor can not initialize the base part.

class Base
{
public:
private:
	int _b;
};

class Derived : public Base
{
public:
	//调用默认的派生类构造函数时,
	//在初始化列表调用基类的构造函数
private:
	int _d;
};

int main()
{
	Derived D;//调用默认的派生类构造函数
	return 0;
}

If the user explicitly base class constructor is given (i.e., the base class has no default constructor) in the derived class, the user needs to explicitly given by the constructor of the derived class, and the required initialization to the list of the derived class the corresponding calls the base class constructor.

class Base
{
public:
	Base(int b)//存在参数,或者不全缺省参数
		:_b(b)
	{
		cout << "Base" << endl;
	}
private:
	int _b;
};

class Derived : public Base
{
public:
	//调用默认的派生类构造函数时,(基类中构造函数参数必须存在,并且为不全缺省)
	//在初始化列表调用基类的构造函数
private:
	int _d;
};

int main()
{
	Derived d;//调用默认的派生类构造函数
	return 0;
}

The reason is because: if no explicit parameter passing, then calling the constructor, the base class parameters can not be determined, so called on demand parameter passing
a derived class object initialization function first calls the base class constructor call in the derived class constructor.

2. destructor

Derived class destructor will carry out their destructor, then calls the destructor destructor the base part at the end of their base class destructor.
If the derived class destructor is not given, the default will be to generate a (simple) destructor, and the default destructor destructor end of the call the base class in a derived class.

class Base
{
public:
private:
	int _b;
};

class Derived : public Base
{
public:
private:
	int _d;
};
int main()
{
	Derived d;
	//会析构派生类对象d,调用默认的派生类析构函数
}

Copy constructor

Derived class (default) copy constructor must call the base class copy constructor configured to copy the object.

Assignment Operator Overloading

operator = the same as the copy constructor, assignment operator needs to call overloading the base class to complete.

Published 52 original articles · won praise 26 · views 3416

Guess you like

Origin blog.csdn.net/weixin_43796685/article/details/103132841