C++实现一个不能被继承的类
来源:互联网 发布:mac环境变量设置 编辑:程序博客网 时间:2024/06/07 17:58
构造函数是实现继承的关键,子类对象在构造时,首先调用父类的构造函数,在调用自己的构造函数。
#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;
定义一个友元类friend T,B在继承的时候,用自己去具体化A,将自己设置为A的友元类,这样就可以访问A的私有函数(构造函数),B就可以正常定义对象b,C虚继承B,但是友元关系不能继承。所以C在构造时,不能调用A的构造函数,如果打开注释部分,将报下边的错误。
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(){}
^
0 0
- 【c++】实现一个不能被继承的类
- C++实现一个不能继承的类
- 试题:实现一个不能被继承的类(上)
- 试题:实现一个不能被继承的类(下)
- 试题:实现一个不能被继承的类(续之下)
- 试题:实现一个不能被继承的类(续之上)
- 用C++实现一个不能被继承的类
- 用C++实现一个不能被继承的类
- C++实现一个不能被继承的类
- C++实现一个不能被继承的类
- 用C++实现一个不能被继承的类
- C++实现一个不能被继承的类
- c++中如何实现一个不能被继承的类
- 面试题:C++实现一个不能被继承的类
- 设计一个不能被继承的类
- 设计一个不能被继承的类
- 设计一个不能被继承的类
- 设计一个不能被继承的类
- 资源池(数据库连接池,内存池,线程池)
- 优雅的代码---浅谈注释的重要性
- OpenCV的Mat基本用法
- 动画类型
- Myeclipse程序调试快捷键及步骤详解
- C++实现一个不能被继承的类
- Android之Handler、Looper、Message探索
- 在测试phone的app中遇到的问题
- 多选框级联权限保存
- Ligerui grid 合并单元格
- 【什么是渲染目标(render target)】
- Linux切换用户提示This account is currently not available
- 深入理解java内部类和匿名内部类
- Delphi中的T