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 ...
随机推荐
- TCP/IP、Http、Soap三个基本的通讯协议
看到一个说法,比较通俗易懂: HTTP就是邮局的协议,他们规定了你的信封要怎么写,要贴多少邮票等.... SOAP就是你们之间交流的协议,负责把你所需要表达的意思写在信纸上,同时也负责 ...
- Spring.NET入门
Spring.NET入门 http://www.cnblogs.com/haogj/archive/2011/06/10/2077540.html http://www.cnblogs.com/ha ...
- MarkDown 语言简单使用
# Markdown file 是Python的标准 ...
- Java入门到精通——框架篇之Hadoop概述
一.Hadoop来历 Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明了倒排索引算法,通过加入了Map ...
- oracle查询所有用户表的表名、主键名称、索引、外键等
1.查找表的所有索引(包括索引名,类型,构成列): select t.*,i.index_type from user_ind_columns t,user_indexes i where t.ind ...
- 如何在PB中调用 Microsoft WEB 浏览器 控件?
PB中使用Microsoft Web Browser控件步骤: 在pb的某窗口中加入OLE对象,选择Insert control(插入控件),然后选中"Microsoft WEB 浏览器&q ...
- C# 任务并行库使用小计 z
1.简单创建使用 using System; using System.Diagnostics; using System.Threading; using System.Threading.Task ...