Constructors are the key to implementing inheritance, when a subclass object is constructed, the constructor of the parent class is called first, and its own constructor is called.
#include <iostream>using namespace std;template <typename t>class a{public : friend T; Private: A () {} ~a () {}};class b:virtual public A <b>{public : B () {} ~b () {}};//class C: Virtual public b//{// public:// C () {}// ~c () {}//};int main () { B b;// c C;
Define a friend class friend T,b in the inheritance, with their own to materialize a, set themselves to a friend class, so that you can access A's private function (constructor), B can normally define the object B,c virtual inheritance B, but the friend relationship can not inherit. So c cannot call A's constructor at construction time, and if you open the comment section, the error below will be reported.
Template.cpp:In constructor ' C::c () ':
Template.cpp:11:3: Error: ' A<t>::a () [With T = B] ' is private
A () {}
^
Template.cpp:25:6: Error:within This context
C () {}
^
Template.cpp:12:3: Error: ' A<t>::~a () [With T = B] ' is private
~a () {}
^
Template.cpp:25:6: Error:within This context
C () {}
^
Template.cpp:In destructor ' c::~c () ':
Template.cpp:12:3: Error: ' A<t>::~a () [With T = B] ' is private
~a () {}
^
Template.cpp:26:7: Error:within This context
~c () {}
^
Implement a class that cannot be inherited in C + +