[英]C++ function pointer in template
I've been assigned the following template: 我被分配了以下模板:
#include <map>
template <typename T>
class Catalog {
struct Item {
//..
};
std::map<int, Item*> items;
public:
Catalog(void);
Catalog(const Catalog&);
~Catalog(void);
bool IsEmpty(void) const;
int Size() const;
void Add(T*);
T* Remove(T*);
T* Find(T*);
typedef void (T::*pFunc) (const T&);
void Inspection(pFunc) const;
};
Next, there is an abstract Product class and three subclasses: 接下来,是一个抽象类产品和三个子类:
class Product {
protected:
unsigned int _id;
string _name;
public:
Product(const int& id, const string& name) : _id(id), _name(name) {};
virtual void Action(const Product& p) = 0;
virtual int hashCode() {
return _id*100;
};
unsigned int getId(void) const {return _id;};
string getName(void) const {return _name;};
};
class ProductA : public Product {
public:
ProductA(const int& id, const string& name) : Product(id, name) {};
virtual void Action(const Product& p) {
cout << "ahoj" << endl;
};
};
Finally, class ProductsCatalog that handles a Catalog instance: 最后,类ProductsCatalog一个处理一个目录实例:
class ProductsCatalog {
Catalog<Product> catalog;
public:
//..
void CatalogInspection(void) const {
catalog.Inspection(&Product::Action);
}
};
What I have trouble with is the Inspection method: 我遇到的麻烦是检查方法:
template <typename T> void Catalog<T>::Inspection(pFunc p) const {
for (std::map<int, Item*>::const_iterator it=items.begin(); it!=items.end(); ++it) {
it->second->Product->*p(*(it->second->Product));
}
};
I am getting the following error: 我收到以下错误:
error C2064: term does not evaluate to a function taking 1 arguments
I've tried everything I could think of, without success. 我尝试了所有我能想到的,但没有成功。 The following works as intended, but is obviously not abstract enough:
如预期,但显然是不够的抽象了以下工作:
it->second->Product->Action(*it->second->Product);
Did you try (it->second->Product->*p)(*(it->second->Product));
您是否尝试过
(it->second->Product->*p)(*(it->second->Product));
for calling the method? 用于调用方法? The thread Calling C++ class methods via a function pointer seems to be related.
通过函数指针调用C ++类方法的线程似乎是相关的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.