- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
不久前,我了解了 Curiously Recurring Template Pattern ( http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern ),它让我想起了我用来实现事件队列缓存的技术。
基本思想是我们利用基类指针来存储同类指针类型的容器。但是因为Derived类是一个模板类,它存储了一个T类型的item,所以我们真正存储的是一个异构类型的列表。
我很好奇是否有人见过这种技术,这可能很有趣,如果是的话,是否有人为它命名?有人愿意批评它吗?有没有更好的方法来达到我的目的?
谢谢。
#include <iostream>
#include <algorithm>
#include <functional>
#include <list>
#include <string>
class Base
{
public:
Base(){}
virtual ~Base(){}
virtual void operator()() = 0;
};
template<typename C, typename T>
class Derived : public Base
{
public:
Derived(C* c, T item) : consumer_(c), item_(item) {}
virtual void operator()()
{
consumer_->consume(item_);
}
C* consumer_;
T item_;
};
class Consumer
{
bool postpone_;
std::list<Base*> cache_;
public:
Consumer() : postpone_(true)
{
}
void pause()
{
postpone_ = true;
}
void resume()
{
postpone_ = false;
const std::list<Base*>::iterator end = cache_.end();
for ( std::list<Base*>::iterator iter = cache_.begin();
iter != end;
++iter )
{
Base* bPtr = *iter;
bPtr->operator()();
delete bPtr;
}
cache_.clear();
}
void consume(int i)
{
if ( postpone_ )
{
std::cerr << "Postpone int.\n";
cache_.push_back(new Derived<Consumer, int>(this, i));
}
else
{
std::cerr << "Got int.\n";
}
}
void consume(double d)
{
if ( postpone_ )
{
std::cerr << "Postpone double.\n";
cache_.push_back(new Derived<Consumer, double>(this, d));
}
else
{
std::cerr << "Got double.\n";
}
}
void consume(char c)
{
if ( postpone_ )
{
std::cerr << "Postpone char.\n";
cache_.push_back(new Derived<Consumer, char>(this, c));
}
else
{
std::cerr << "Got char.\n";
}
}
};
static Consumer consumer;
void destroy(Base* object)
{
delete object;
}
int main()
{
// Consumer is registered with something that sends events out to lots
// of different consumer types (think observer pattern). Also in the non-toy
// version consumer isn't being passed PODs, but various Event types.
consumer.consume(0);
consumer.consume(0.1f);
consumer.consume('x');
consumer.resume();
}
输出是:
Postpone int.
Postpone double.
Postpone char.
Got int.
Got double.
Got char.
最佳答案
正如斯蒂芬在他的评论中指出的那样,您使用的是简单的多态性。当您在容器内部存储不同的对象时,您只能使用 Base
中定义的接口(interface)。也就是说,当然,除非您打算添加类型检查和向下转换以实际检索值。对于不相关的对象,您只能做有限的事情。
根据您实际想要实现的目标,您可能会考虑使用其他解决方案,例如 boost::any
/boost::variant
如果您想要实际存储不相关的类型(在少数有意义的情况下——例如电子表格中的单元格)。
关于c++ - 使用模板和虚函数的技巧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3152415/
我是新手。查看 Google 新闻...上下滚动页面时请注意左侧导航栏。 看看它是如何滚动一点,然后在它消失之前粘在页面顶部的? 关于如何做到这一点有什么想法吗? jQuery 和 CSS 可以复制吗
技巧 1:在 Web 服务器上缓存常用数据 技巧 2:在 Application 或 Session 对象中缓存常用数据 技巧 3:在 Web 服务器磁盘上缓存数据和 HTML 技巧 4:避免
我在 excel 中有一个电子表格,其中包含以下行: COLUMN Value1.Value2.Value3 Value4.Value5.Value6 Value7.Value8.Val
GNU Makefile 中是否有任何技巧来获取规则的所有依赖项? 例子: rule1: dep1_1 dep1_2 dep1_3 rule2: dep2_1 dep2_2 rule1 dump_
人们使用什么来追踪内存泄漏?我已经通过代码检查设法解决了一些问题,但我不知道下一步该做什么/当我的程序变大时我将如何管理问题。我知道我在泄漏什么类型的对象,但我不知道是什么让它保持活力。 在 Wind
有什么好的方法可以将“xlSum”、“xlAverage”和“xlCount”等字符串转换为它们在 Microsoft.Office.Interop.Excel.XlConsolidationFunc
我们都见过这个: javascript:document.body.contentEditable='true'; document.designMode='on';无效 0 但我的问题是,这实际上是
我的应用程序将输出一个图形,其布局由用户定义。自定义布局类应该实现我定义的接口(interface)。我应该怎么做?有一个特殊的文件夹,我可以在其中查找布局类?用户是否将类名作为参数传递给应用? 如有
我在弄清楚如何在 Javascript 中自引用表行时遇到了一些麻烦。 这是简化的代码: $( "#listitems tbody" ).append( "" + "" + id.va
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this q
我正在将代码库从一种编程风格转移到另一种编程风格。 我们有一个名为 Operand 的类型,定义如下: class Operand {...}; 然后我们有 class OperandFactory
我使用以下缩略图类在我的内容包装器中显示 4x3 缩略图: .thumbnail { float:left; width:300px; height:200px; ma
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我认为这是不可能的,但我想在放弃之前问问你。 我想要类似 constexpr 增量的东西。 #include constexpr int inc() { static int inc = 0;
是否有任何适合 C++ 新手的技术和描述的好列表。我在想一个描述 RAII、RVO、左值的列表……这适用于目前不了解这些技术或来自不适用这些技术的其他语言的新手。 最好是短小精悍的:-) 最佳答案 是
我有一个二进制字符串 '01110000',我想在不编写 forloop 的情况下返回前面的前导零数。有谁知道如何做到这一点?如果字符串立即以“1”开头,最好也返回 0 最佳答案 如果您真的确定它是一
我需要优化我的应用程序的 RAM 使用率。 请省去那些告诉我在编写 Python 代码时不应该关心内存的讲座。我有内存问题,因为我使用非常大的默认字典(是的,我也想快点)。我目前的内存消耗是 350M
有时,当我看到一个我喜欢的网站或来自受人尊敬的人的网站时,我会查看源代码并尝试理解它们(就像我们所有人一样)。 关于 Jeremy Keiths他使用以下代码的网站: [role="navigatio
这是我怎样设置 Git 来管理我的家目录的方法。 我有好几台电脑。一台笔记本电脑用于工作,一台工作站放在家里,一台树莓派(或四台),一台 Pocket CHIP,一台 运行
shell 技巧 表变量 HBase 0.95 版本增加了为表提供 jruby 风格的面向对象引用的 shell 命令。以前,作用于表的所有 shell 命令都具有程序风格,该风格始终将表的名称作
我是一名优秀的程序员,十分优秀!