有些时候我们需要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. TCP/IP、Http、Soap三个基本的通讯协议

    看到一个说法,比较通俗易懂: HTTP就是邮局的协议,他们规定了你的信封要怎么写,要贴多少邮票等....         SOAP就是你们之间交流的协议,负责把你所需要表达的意思写在信纸上,同时也负责 ...

  2. Spring.NET入门

    Spring.NET入门  http://www.cnblogs.com/haogj/archive/2011/06/10/2077540.html http://www.cnblogs.com/ha ...

  3. MarkDown 语言简单使用

    # Markdown file ![alt img is error](http://cdn2.jianshu.io/assets/web/logo-58fd04f6f0de908401aa561cd ...

  4. 译:面试投行的20个Java问题

    原文链接:https://dzone.com/articles/var-work-in-progress 作者:Anghel Leonard 译者:沈歌 如果你需要准备面试,可以看一下这篇博客中20个 ...

  5. 有关table布局时tr 属性display:block显示布局错乱

    display:block display:block是可以把非块级元素强制转换为块级元素显示,如内嵌元素span,原来不支持设置宽高,宽度是由内容撑开的; display:table-row tab ...

  6. Stage5--Python GUI编程TKinter

    Python图形库简要介绍 python提供了多个图形开发界面的库,几个常用Python GUI库如下: Tkinter: Tkinter模块("Tk 接口")是Python的标准 ...

  7. Java入门到精通——框架篇之Hadoop概述

    一.Hadoop来历 Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明了倒排索引算法,通过加入了Map ...

  8. oracle查询所有用户表的表名、主键名称、索引、外键等

    1.查找表的所有索引(包括索引名,类型,构成列): select t.*,i.index_type from user_ind_columns t,user_indexes i where t.ind ...

  9. 如何在PB中调用 Microsoft WEB 浏览器 控件?

    PB中使用Microsoft Web Browser控件步骤: 在pb的某窗口中加入OLE对象,选择Insert control(插入控件),然后选中"Microsoft WEB 浏览器&q ...

  10. C# 任务并行库使用小计 z

    1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...