构造函数是实现继承的关键,子类对象在构造时,首先调用父类的构造函数,在调用自己的构造函数。
#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;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(){}
^
原文地址:http://blog.csdn.net/xueyuehanzhu123/article/details/44079145