有些时候我们需要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的更多相关文章

  1. Resource Acquisition Is Initialization(RAII Idiom)

    原文链接:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Resource_Acquisition_Is_Initialization Intent ...

  2. [Effective Modern C++] Item 6. Use the explicitly typed initializer idiom when auto deduces undesired types - 当推断意外类型时使用显式的类型初始化语句

    条款6 当推断意外类型时使用显式的类型初始化语句 基础知识 当使用std::vector<bool>的时候,类型推断会出现问题: std::vector<bool> featu ...

  3. idiom - Initialization-on-demand holder 延迟加载的单例模式

    参考:https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom idiom - 一个线程安全的.无需synchroniza ...

  4. C++进阶--Named Parameter Idiom

    //############################################################################ /* Named Parameter Id ...

  5. 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 ...

  6. 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 ...

  7. pimpl idiom

    pimpl idiom flyfish 2014-9-30 pimpl是Pointer to implementation的缩写 为什么要使用pimpl 1最小化编译依赖 2接口与实现分离 3可移植 ...

  8. Pimpl Idiom /handle body idiom

    在读<Effective C++>和项目源代码时,看到pImpl Idiom.它可以用来降低文件间的编译依赖关系,通过把一个Class分成两个Class,一个只提供接口,另一个负责实现该接 ...

  9. copy-and-swap idiom

    This answer is from https://stackoverflow.com/a/3279550/10133369 Overview Why do we need the copy-an ...

随机推荐

  1. html元素固定

    1.position 值 描述 static        默认.位置设置为 static 的元素,它始终会处于页面流给予的位置(static 元素会忽略任何 top.bottom.left 或 ri ...

  2. select(有局限性),jq循环添加select的值

    加载的时候改变select的默认值,只需改变select的value值 $("#one").val(@ViewBag.val);//@ViewBag.val是要默认选中的值的val ...

  3. ZROJ#397. 【18提高7】模仿游戏(爆搜)

    题意 题目链接 Sol 考试的时候调了1.5h没调出来我真是菜爆了... 读完题目后不难发现,每次约束的条件相当于是\(b[((x[i] + i) % N + (i / N) % N) % N] = ...

  4. TopcoderSRM679 Div1 250 FiringEmployees(树形dp)

    题意 [题目链接]这怎么发链接啊..... 有一个 \(n\) 个点的树,每个点有点权(点权可能为负) ,求包含点\(1\)的最 大权连通子图(的权值和) . \(n \leqslant 2500\) ...

  5. javascript实现数据结构:稀疏矩阵的十字链表存储表示

    当矩阵的非零个数和位置在操作过程中变化大时,就不宜采用顺序存储结构来表示三元组的线性表.例如,在作“将矩阵B加到矩阵A上”的操作时,由于非零元的插入或删除将会引起A.data中元素的移动.为此,对这种 ...

  6. 【起航计划 037】2015 起航计划 Android APIDemo的魔鬼步伐 36 App->Service->Remote Service Binding AIDL实现不同进程间调用服务接口 kill 进程

    本例和下个例子Remote Service Controller 涉及到的文件有RemoteService.java ,IRemoteService.aidl, IRemoteServiceCallb ...

  7. C++中char*与wchar_t*之间的转换

    http://blog.163.com/tianshi_17th/blog/static/4856418920085209414977/ 关于C++中的char*与wchar_t*这两种类型的相互转换 ...

  8. Gson 转换hibernate级联对象出现StackOverFlow(堆栈溢出)问题

    < many-to-one>和< one-to-many>属性的对象级联关系在转换时会造成死循环,报stackOverFlowException. 比如下面这段: @OneTo ...

  9. greenplum维护

    1.用户管理 psql -d sea CREATE DATABASE BI; CREATE USER ubi WITH PASSWORD 'pwdbi' NOSUPERUSER; GRANT ALL ...

  10. <context:property-placeholder>配置资源文件

    直接在 spring 配置文件里面加上 <context:property-placeholder file-encoding="UTF-8" location=" ...