【问题标题】:Array of templated type in C++C ++中的模板类型数组
【发布时间】:2011-08-17 15:12:22
【问题描述】:

我有以下:

QFutureWatcher<bool> *procwatcher;
procwatcher = new QFutureWatcher<bool>();
QFuture<bool> procfuture = QtConcurrent::run(this, &EraserBatch::processTile);
procwatcher->setFuture(procfuture);

  QFutureWatcher<bool> *procwatcher2;
  procwatcher2 = new QFutureWatcher<bool>();
  QFuture<bool> procfuture2 = QtConcurrent::run(this, &EraserBatch::processTile);
  procwatcher2->setFuture(procfuture2);

创建这两种类型的动态大小数组的语法是什么 - QFutureWatcher 和 QFuture 以便我可以说 procwatcher[0] 和 procfuture[1] 等。

谢谢!

【问题讨论】:

  • these 是什么意思。你想要一个 指针到 QFutureWatcher&lt;bool&gt; 的动态数组,还是QFutureWatcher&lt;bool&gt; 的动态数组?
  • 编辑了帖子以提供一些上下文

标签: c++ arrays templates


【解决方案1】:

只要模板是完全专用的(即指定所有模板参数),那么您可以简单地这样做:

#include <vector> // required for std::vector

std::vector<QFutureWatcher<bool>*> procWatchers;

尽管根据QFutureWatcherthese documentation examples 中的使用方式,您可能希望将QFutureWatcher 实例存储在std::vector 中:

std::vector<QFutureWatcher<bool> > procWatchers;

这样您就不必手动 newdelete QFutureWatcher 实例。

显然是QFutureWatcher inherits from QObject,也就是uncopyable。这会阻止 std::vector&lt;QFutureWatcher&lt;bool&gt; &gt; 工作。


你有这个:

QFutureWatcher<bool> *procwatcher;
procwatcher = new QFutureWatcher<bool>();
QFuture<bool> procfuture = QtConcurrent::run(this, &EraserBatch::processTile);
procwatcher->setFuture(procfuture);

QFutureWatcher<bool> *procwatcher2;
procwatcher2 = new QFutureWatcher<bool>();
QFuture<bool> procfuture2 = QtConcurrent::run(this, &EraserBatch::processTile);
procwatcher2->setFuture(procfuture2);

你可以这样做:

// Not tested!

// Bundle QFutureWatcher and QFuture together.
template<typename T>
struct FutureStruct
{
    FutureStruct(QFutureWatcher<T>* w, const QFuture<T>& f)
        : watcher(w), future(f)
    {
        this->watcher->setFuture(this->future);
    }

    QFutureWatcher<T>* watcher; // Apparently QObjects can't be copied.
    QFuture<T> future;
};

// ...

std::vector< FutureStruct<bool> > futures;

// ...

void AddFuture()
{
    futures.push_back(FutureStruct<bool>(new QFutureWatcher<bool>(),
        QtConcurrent::run(this, &EraserBatch::processTile)));
}

// ...

futures[0].watcher; // gets you the first QFutureWatcher<bool>*
futures[0].future;  // gets you the first QFuture<bool>

futures[1].watcher; // gets you the second QFutureWatcher<bool>*
futures[1].future;  // gets you the second QFuture<bool>

// ...

当然,因为QFutureWatcher&lt;bool&gt; 被分配了new,你需要在futures 向量消失之前delete 它:

for(std::vector< FutureStruct<bool> >::iterator i = futures.begin();
    i != futures.end(); ++i)
{
    delete i->watcher;
}

【讨论】:

  • 不确定*。 OP 到底想要什么?
【解决方案2】:

如果观察者不仅由向量拥有,那么 RAII 方式:

typedef boost::shared_ptr<QFutureWatcher<bool> > ProcWatcherPtr;
std::vector<ProcWatcherPtr> procWatchers;

如果观察者仅由向量拥有,则采用 RAII 方式:

typedef QFutureWatcher<bool> ProcWatcher
boost::ptr_vector<ProcWatcher> procWatchers;

如果符合您的需要,也可以不分配内存:

std::vector<QFutureWatcher<bool> > procWatchers;

【讨论】:

    猜你喜欢
    • 2010-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2023-03-31
    相关资源
    最近更新 更多