c++ - 从非模板基类指针调用模板派生类方法的方法
问题描述
我正在尝试在我的项目中实现一个类似于 Qt 中的属性系统的属性系统。我们刚开始有一些想法,正处于原型设计阶段。基本上,我从 Qt 中了解到的是,客户端应该能够通过 .h 文件中的一些宏来传递 get 函数、set 函数和属性类型。所以我试着模仿同样的。
以下是我的示例代码:
抽象吸气剂类。这种类型的 getter 类是 Property Class 中的成员
class AbstractFunc
{
public:
template < typename R >
R Invoke ()
{
return (this)->Invoke ();
}
};
获取函数模板:返回值可以是 T 、 T& 、 const T& 、 T* 等。
template < typename R, class T > class GetterFunction : public AbstractFunc
{
typedef R (T::*GetterFunc) ();
public:
GetterFunction (T * obj, GetterFunc func):m_Obj (obj), m_Func (func)
{
}
R Invoke ()
{
return m_Obj->*(m_Func) ();
}
public:
T * m_Obj;
GetterFunc m_Func;
};
属性类:
class Property
{
public:
Property (string name, AbstractFunc* getter):m_name (name), m_getter (getter)
{
}
template < typename R > R GetValue ()
{
return m_getter->Invoke < R > ();
}
private:
string m_name;
AbstractFunc* m_getter;
};
一些窗口类:
class Window
{
public:
};
示例窗口类
class CheckBox :public Window
{
public:
int GetChecked ()
{
return m_checked;
}
void SetChecked (int nChecked)
{
m_checked = nChecked;
}
void AddProperty (string name)
{
m_prop = new Property (name, new GetterFunction< int, Checked >(this, &Checked::GetChecked));
}
int m_checked;
Property *m_prop;
};
主功能:
int main ()
{
CheckBox cc;
cc.AddProperty ("Hello");
cout<<"value:"<< cc.m_prop->GetValue<int>();
return 0;
}
问题:Getter 函数在属性类中被记住为 AbstractFunc。我想在 AbstractFunc* 实例上调用“调用”,它应该调用成员函数并返回正确的返回类型。上面的代码在 AbstractFunc::Invoke 处抛出错误。
解决方案
你AbstractFunc
根本不是抽象的:它Invoke
不是虚拟的。因此,即使GetterFunction
还有一个名为 的方法Invoke
,该方法实际上并没有覆盖AbstractFunc::Invoke
;它只是隐藏它。当您尝试Invoke
通过 调用时AbstractFunc*
,它会调用AbstractFunc::Invoke
,这会进入无限递归并因此产生 UB。
我会按照@nm 的建议创建一个像这样的类层次结构:
class AbstractFunc {
// lock down construction
AbstractFunc() = default;
public:
template<typename R>
R Invoke();
template<typename R>
bool HasType() const noexcept;
virtual ~AbstractFunc() = default; // need to have SOME virtual method so that we have runtime type info; also a virtual destructor is required anyway
template<typename R>
friend class TypedFunc;
};
template<typename R>
struct TypedFunc : AbstractFunc { // the ONLY instances of AbstractFunc are also instances of specializations of TypedFunc
virtual R InvokeTyped() = 0;
};
// one kind of TypedFunc applies a getter on an object
template<typename R, typename T>
struct GetterFunc : TypedFunc<R> {
// you never see a GetterFunc in the interface anyway... don't see a need to hide these
T *obj; // have you considered std::shared_ptr?
R (T::*getter)();
GetterFunc(T *obj, R (T::*getter)()) : obj(obj), getter(getter) { }
R InvokeTyped() override { return (obj->*getter)(); }
};
template<typename R, typename T>
std::unique_ptr<GetterFunc<R, T>> MakeGetterFunc(T *obj, R (T::*getter)()) {
return std::make_unique<GetterFunc<R, T>>(obj, getter);
}
// another kind applies a functor, etc.
template<typename R, typename F>
struct FunctorFunc : TypedFunc<R> {
F func;
template<typename... G>
FunctorFunc(G&&... args) : func(std::forward<G>(args)...) { }
R InvokeTyped() override { return func(); }
};
这已经可以使用了:如果您有 anAbstractFunc*
或 an AbstractFunc&
,您可以dynamic_cast
将其缩减为TypedFunc
预期类型的 a (例如TypedFunc<int>
)。如果成功(您得到一个非空指针或没有std::bad_cast
异常),那么您只需调用而无需知道您实际处理的是InvokeTyped
哪种GetterFunc
// 。FunctorFunc
函数Invoke
和HasType
声明AbstractFunc
的只是帮助做到这一点的糖。
template<typename R>
bool AbstractFunc::HasType() const noexcept {
return dynamic_cast<TypedFunc<R> const*>(this);
}
template<typename R>
R AbstractFunc::Invoke() {
return dynamic_cast<TypedFunc<R>&>(*this).InvokeTyped();
// throws std::bad_cast if cast fails
}
完毕。
class Property {
std::string name;
std::unique_ptr<AbstractFunc> getter;
public:
Property(std::string name, std::unique_ptr<AbstractFunc> getter) : name(std::move(name)), getter(std::move(getter)) { }
template<typename R>
bool HasType() const noexcept { return getter->HasType<R>(); }
template<typename R>
R GetValue() const { return getter->Invoke<R>(); }
std::string const &GetName() const noexcept { return name; }
};
struct Window {
virtual ~Window() = default;
// doesn't really make sense to add/remove these from outside...
virtual std::vector<Property> GetProperties() = 0;
};
class CheckBox : public Window {
int checked = 0;
public:
int GetChecked() /*const*/ noexcept { return checked; }
void SetChecked(int checked) noexcept { this->checked = checked; }
std::vector<Property> GetProperties() override {
std::vector<Property> ret;
ret.emplace_back("Boxes Checked", MakeGetterFunc(this, &CheckBox::GetChecked));
return ret;
}
};
int main() {
CheckBox cb;
cb.SetChecked(5);
for(auto const &prop : cb.GetProperties()) std::cout << prop.GetName() << ": " << prop.GetValue<int>() << "\n";
}
然后,如果您希望能够直接获取类型等,则可以添加例如 avirtual std::type_info const& GetType() const
或类似内容。AbstractFunc
推荐阅读
- android - Android,从图库中获取图像到 imageView
- cordova - 剑道组合框清除按钮不适用于android 9
- elasticsearch - Elasticsearch 根据 2 个标准返回文档,其中一个基于另一个
- angular - 使用 Open ID Connect 访问令牌保护 Web API
- javascript - 如何用点将日期 19990813 (YYYY-MM-DD) 格式化为 13.08.1999 (DD-MM-YYYY)?
- ios - 获取用户当前位置/获取经纬度值 watchkit/ios swift
- vba - 通过 VBA 打开由查询生成的报表,并为查询输入数据以创建表单
- nginx - ngix 重定向到 ubuntu 服务器中的非 www
- php - 在 Debian 上安装 php7.1-bcmath
- python - 有没有办法在没有终端的情况下使用 glade 应用程序?