背景:我正在使用委托技术来抽象访问任意对象方法,但我在链接器方面存在一些问题。考虑以下课程,ContextNode
。无法解析模板委托中的重载类方法
template <class ObjectType, class GetType, class SetType>
class ContextNode: public ContextNodeBase {
public:
ContextNode(ObjectType* target,
GetType (ObjectType::*getter)(void),
void (ObjectType::*setter)(const SetType& ref)
): _target(target), _getter(getter), _setter(setter) { }
virtual ~ContextNode(void) { }
virtual void r(Datum& value) {
value = (_target->*_getter)();
return;
}
virtual void w(const Datum& value) {
(_target->*_setter)(value);
return;
}
private:
ObjectType* _target;
GetType (ObjectType::*_getter)(void);
void (ObjectType::*_setter)(const SetType& ref);
};
Datum
的执行是无关紧要的。也考虑微不足道的类Thing
。
class Thing {
public:
Thing(void);
~Thing(void);
int getValue(void) { return _value; }
void setValue(const int& x) { _value = x; }
private:
int _value;
};
问题:我可以构建的ContextNode
实例,像这样。
Thing* thing = new Thing();
ContextNode<Thing,int,int>* cn = new ContextNode<Thing,int,int>(thing, &Thing::getValue, &Thing::setValue);
这适用于我的需要。我遇到问题,但重载的方法。假设我写了:
class Thing {
public:
Thing(void);
~Thing(void);
int value(void) { return _value; }
void value(const int& x) { _value = x; }
private:
int _value;
};
Thing* thing = new Thing();
ContextNode<Thing,int,int>* cn = new ContextNode<Thing,int,int>(thing, &Thing::value, &Thing::value);
这未能链接。我相信这个问题是链接器仅尝试基于名称的解析,因此我看到了<unresolved overloaded function type>
错误。
我的问题:是否有一些语法糖来显式指定我指的几个重载方法中的哪一个?我无法想象这样一个愚蠢的怪癖会打破这样一个优雅的解决方案。我在网上找不到任何东西,也没有找到C++常见问题解答,也没有关于这个话题的SO。
什么是修复程序,或者我洗过?
好主意,但是'错误:没有上下文类型信息的重载函数的地址' – 2010-10-16 02:09:17
哦,具体来说,这个错误来自于getter的转换,而不是setter。 – 2010-10-16 02:28:25
Bah - 我的错。我有一些常量声明是错误的。谢谢! – 2010-10-16 02:34:58