c++中模板类型的数组
Array of templated type in C++
我有以下内容:
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]等等。
谢谢!
只要模板是完全专门化的(即指定了所有模板参数),那么你可以简单地这样做:
#include <vector> // required for std::vector
std::vector<QFutureWatcher<bool>*> procWatchers;
尽管根据QFutureWatcher
在这些文档示例中的使用方式,您可能希望将QFutureWatcher
实例存储在std::vector
中:
std::vector<QFutureWatcher<bool> > procWatchers;
这样你就不需要手动new
和delete
QFutureWatcher
实例。显然QFutureWatcher
继承自QObject
,这是不可复制的。这将阻止std::vector<QFutureWatcher<bool> >
工作。
你有这个:
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<bool>
是与new
一起分配的,所以您需要在futures
向量消失之前将其delete
:
for(std::vector< FutureStruct<bool> >::iterator i = futures.begin();
i != futures.end(); ++i)
{
delete i->watcher;
}
如果观察者不仅属于vector:
typedef boost::shared_ptr<QFutureWatcher<bool> > ProcWatcherPtr;
std::vector<ProcWatcherPtr> procWatchers;
如果观察者只属于vector:
typedef QFutureWatcher<bool> ProcWatcher
boost::ptr_vector<ProcWatcher> procWatchers;
或不分配内存,如果它适合您的需要:
std::vector<QFutureWatcher<bool> > procWatchers;
相关文章:
- 声明C++具有动态大小的数组类型在 Linux 中工作正常,但不能在 Windows 中工作
- 数组类型 int[n][n] 不可赋值,因为表达式必须具有常量值
- 取消引用结束指针到数组类型的一个
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 如何在数组类型的模板代码中计算std::size_t
- LLVM 无法将数组类型强制转换为常量数组
- 推荐的方法在不初始化值的情况下使数组类型为 std::unique_ptr?
- 特征:从数组类型中获取标量类型是否记录?
- T D[N] 总是声明数组类型的对象吗?
- 如何在模板中推断数组类型?
- 尝试使用 Array 类的模板构造整数数组时,错误:为什么数组类型"int [5]"不可分配,
- 如何将二维数组类型字符(字符串)作为函数参数传递?
- 如何为所有非数组类型专门化模板?
- 数组类型 (C++) 的初始化
- 我预测char数组类型值只能投射指针类型
- 将数组类型更改为 Int16 会节省存储空间吗?
- 采用通用数组类型的函数
- 数组类型 'int [8]'不可分配
- 数组类型 'float [size]'不可分配
- C++。候选模板被忽略 - 无法将数组类型与自身匹配