有些时候我们需要upcast为多种类型,这种情况下除了可以使用multiply inherits还可以inner class。
以下为例子:

//: C10:InnerClassIdiom.cpp
// Example of the "inner class" idiom.
#include <iostream>
#include <string>
using namespace std; class Poingable {
public:
virtual void poing() = ;
};
void callPoing(Poingable& p) {
p.poing();
} class Bingable {
public:
virtual void bing() = ;
};
void callBing(Bingable& b) {
b.bing();
} class Outer {
string name;
// Define one inner class:
class Inner1;
friend class Outer::Inner1;
class Inner1 : public Poingable {
Outer* parent;
public:
Inner1(Outer* p) : parent(p) {}
void poing() {
cout << "poing called for "
<< parent->name << endl;
// Acesses data in the outer class object
}
} inner1; // Define a second inner class:
class Inner2;
friend class Outer::Inner2;
class Inner2 : public Bingable {
Outer* parent;
public:
Inner2(Outer* p) : parent(p) {}
void bing() {
cout << "bing called for "
<< parent->name << endl;
}
} inner2;
public:
Outer(const string& nm)
: name(nm), inner1(this), inner2(this) {}
// Return reference to interfaces
// implemented by the inner classes:
operator Poingable&() { return inner1; }
operator Bingable&() { return inner2; }
}; int main() {
Outer x("Ping Pong");
// Like upcasting to multiple base types!:
callPoing(x);
callBing(x);
} ///:~

注意inner class的使用方法:1、前置声明 2、friend  3、定义 4、访问private变量

内容源自:《TICPP-2nd-ed-Vol-two》

最新文章

  1. 图片在保存的时候===》出现这个异常:GDI+ 中发生一般性错误
  2. 解析opencv中Box Filter的实现并提出进一步加速的方案(源码共享)。
  3. 《learning hard C#学习笔记》读书笔记(19)多线程
  4. YbSoftwareFactory 代码生成插件【十七】:先进的权限模型体系设计
  5. OC中用NSSortDescriptor对象进行数组排序
  6. IEnumerable 遍历用法
  7. return exit _exit
  8. 用php做了下冒泡排序
  9. zabbix企业级监控概述和部署
  10. CocoaPods requires your terminal to be using UTF-8 encoding
  11. mac 下 配置 阿帕奇
  12. kubernetes in action - Volumes
  13. (逆序对 分治法)P1908 逆序对 洛谷
  14. $Django cbv源码分析 djangorestframework框架之APIView源码分析
  15. SpringMVC国际化与文件上传
  16. LOJ10155数字转换
  17. ps怎么撤销的三种方法和ps撤销快捷键以及连续撤销多步快捷键
  18. pyhanlp 共性分析与短语提取内容详解
  19. MFC使用自定义资源加载PNG
  20. [Java学习] Java继承的概念与实现

热门文章

  1. PHP通过映射类查找类所在文件
  2. Nginx下载安装
  3. 使用mermain用Markdown的语法画流程图和UML图
  4. MVC5 下拉框绑定(单选)
  5. 【Java集合】LinkedList详解前篇
  6. es6变量解构赋值的用途
  7. Hadoop的安装与配置(虚拟机中的伪分布模式)
  8. Python学习系列----第五章 模块
  9. bit_count
  10. 面向对象设计之------Is-A(继承关系)、Has-A(合成关系,组合关系)和Use-A(依赖关系)(转)