我有一个接口ICollection实现了这样的集合ArdalanCollection:
template <typename T>
class ICollection
{
public:
virtual void add(T*) = 0;
virtual T* get(int) = 0;
virtual int count() = 0;
};
template <typename T>
class ArdalanCollection :public ICollection<T>
{
public:
ArdalanCollection() {
index = 0;
};
virtual void add(T* obj) {
encapsolateImplementation.insert(make_pair(index++, obj));
};
virtual T* get(int index) {
return encapsolateImplementation[index];
};
virtual int count() {
return encapsolateImplementation.size();
};
private:
int index;
unordered_map < int, T* > encapsolateImplementation;
};我想要的是在ICollection接口中有一个通用迭代器,它可以循环所有内部容器元素(我还没有决定选择unordered_map作为我的内部容器,我可能会将它更改为boost或其他东西)。我想用这样的方式:
Node *node1 = new Node(1, 0, 0, 0);
Node *node2 = new Node(1, 0, 0, 0);
ICollection<Node> *nodes = new ArdalanCollection<Node>();
nodes->add(node1);
nodes->add(node2);
for (it=nodes->iterator.begin(); it < nodes->iterator.end(); it++) {
}发布于 2015-07-10 16:16:59
首先,您的for循环成语是不正确的。它应该看起来像
for(auto it = nodes->begin(); it != nodes->end(); it++)然后是这样的:
template <typename T, typename MyMap>
class ICollection
{
public:
typedef typename MyMap<int, T *>::iterator iterator;
virtual void add(T*) = 0;
virtual T* get(int) = 0;
virtual int count() = 0;
};应该没事的。
https://stackoverflow.com/questions/30517260
复制相似问题