gpt4 book ai didi

c++ - 使用模板和虚函数的技巧

转载 作者:行者123 更新时间:2023-11-30 01:31:22 25 4
gpt4 key购买 nike

不久前,我了解了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com