the “inner class” idiom
有些时候我们需要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》
the “inner class” idiom的更多相关文章
- Resource Acquisition Is Initialization(RAII Idiom)
原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization Intent ...
- [Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句
条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...
- idiom - Initialization-on-demand holder 延迟加载的单例模式
参考:https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom idiom - 一个线程安全的.无需synchroniza ...
- C++进阶--Named Parameter Idiom
//############################################################################ /* Named Parameter Id ...
- Idiom: a Lot on my Plate
Idiom: a Lot on my Plate Share Tweet Share Tagged With: Idioms I’ve got a lot on my plate. American ...
- How to Pronounce the Idiom: ‘Out Like a Light’
How to Pronounce the Idiom: ‘Out Like a Light’ Share Tweet Share Tagged With: Idioms English is full ...
- pimpl idiom
pimpl idiom flyfish 2014-9-30 pimpl是Pointer to implementation的缩写 为什么要使用pimpl 1最小化编译依赖 2接口与实现分离 3可移植 ...
- Pimpl Idiom /handle body idiom
在读<Effective C++>和项目源代码时,看到pImpl Idiom.它可以用来降低文件间的编译依赖关系,通过把一个Class分成两个Class,一个只提供接口,另一个负责实现该接 ...
- copy-and-swap idiom
This answer is from https://stackoverflow.com/a/3279550/10133369 Overview Why do we need the copy-an ...
随机推荐
- linux下为.net core应用创建守护进程
1.Supervisor 安装 yum install python-setuptools easy_install supervisor 2.配置 Supervisor mkdir /etc/sup ...
- C# 自定义属性Attribute
自定义属性 /// <summary> /// 脱敏属性 /// </summary> public class SensitiveAttribute:Attribute { ...
- 阿里巴巴国际站 网站和PC客户端都登录不了,其他电脑或手机可以
背景 昨天晚上,我还能打开阿里巴巴国际站,PC客户端也可以登录 今天早上起床打开电脑,发现国际站的网站打开不了,客户端也登录不了,提示了错误信息,但是其他电脑或手机就可以登录 原因分析 1.是不是本机 ...
- 2、Dubbo源码解析--服务发布原理(Netty服务暴露)
一.服务发布 - 原理: 首先看Dubbo日志,截取重要部分: 1)暴露本地服务 Export dubbo service com.alibaba.dubbo.demo.DemoService to ...
- oracle学习篇十:序列
序列:是用来生成唯一.连续的整数的数据库对象.序列通常用来自动生成主键或唯一键的值. 1. 创建序列语法如下: Create SEQUENCE sequence_name [START WITH in ...
- 【Android】1.0 安卓生猛上手
1.Android操作系统,由安迪鲁宾发明,原发明用于照相机操作系统,市场反应惨淡,需求不足,后2005年被谷歌收购,用于智能手机操作系统. 2.logo来源:设计师上厕所看到男女厕所区别标志联想创作 ...
- onload与ready差异
window.onload: 等所有资源加载完document.ready: DOM树构建完资源还没加载完 应该使用ready保证用户体验.否则当网站有很多图片资源时要很长时间才能加载完这段时间内Js ...
- 统计学中的P值与显著性的意义
统计学意义(p值) 结果的统计学意义是结果真实程度(能够代表总体)的一种估计方法.专业上,p值为结果可信程度的一个递减指标,p值越大,我们越不能认为样本中变量的关联是总体中各变量关联的可靠指标.p值是 ...
- svn图标显示不正常,文件夹显示但文件不显示svn图标
svn图标显示不正常,文件夹显示但文件不显示svn图标 这个问题的引发是自己造成的,使用myEclipse时progress会卡在 refresh svn status cache (0%)这里, ...
- Java—集合框架 List的 indexOf()、lastIndexOf()
集合中某个元素出现的位置—List的indexOf(),lastIndexOf() indexOf(Object obj)方法的实现机制是从序列(List)的第0个元素开始依次循环,并且调用每个元素的 ...