CRTP和返回void的方法*
CRTP and method returning void *
我使用C++11。我有一个Base
类和几个派生类,用于逐行解析不同的配置文件。
template <class T>
class Base
{
public:
virtual ~Base();
bool load_from_file(const QString& str);
virtual void* get_data(const QString& str) const = 0;
private:
QList<QSharedPointer<T> > items_;
};
每个子代(class Derived: public Base<My_struct>
)必须提供get_data()
实现
每个My_struct
实例都包含来自设置文件某行的信息。
例如,想象一个具有代理列表的典型文件My_struct
实例封装在load_from_file()
方法中Base
类的智能指针中,并附加到items_
成员。load_from_file()
方法在包装之前将void*
强制转换为T*
。
是否可以重新设计这些类以避免使用void*
(并且没有像boost::any
这样的库)
我的意思是考虑CRTP等等。通常CRTP示例包含具有void
返回值的派生类的方法(如Pascal中的过程)。
template <typename Derived>
struct base
{
template <typename T>
auto f(T x)
{
return static_cast<Derived&>(*this).f_impl(x);
}
auto g()
{
return static_cast<Derived&>(*this).g_impl();
}
};
struct derived : base<derived>
{
bool f_impl(int x)
{
return true;
}
double g_impl()
{
return 4.2;
}
};
这个碎片是从这里取下来的。
相关文章:
- 如何在C++中使用 void* 在指针作为对方法的引用传递时保存uint32_t的值
- 如何将变量的值安全地传递给C++方法,该方法只将void*作为参数
- 用非零值初始化void指针的正确(或最安全)方法
- 退出c++中的递归void方法
- Arduino trim() 和 replace() 方法返回从 'void' 到非标量类型'String'请求的转换
- 什么是 'R(*pf)(void*, Args..)',函数指向方法的指针?
- 回调方法显示错误,类型为"void(*)"的值不能用于初始化类型的实体
- 将 void* 强制转换为多个继承类的超类不会调用正确的方法
- 指向类模板方法的 void 指针的向量
- 将 void* 强制转换为特定类型的指针的方法签名
- 来自 void 指针的虚拟方法表
- 使用 void 方法将字符串从 txt 文件检索到 int C++的向量
- 将带有参数的void方法传递给使用std ::函数和std :: bind c 的另一种方法
- 如何修复空虚拟方法的"warning: no return statement in function returning non-void [-Wreturn-type]"
- CRTP和返回void的方法*
- 为什么不允许虚方法返回 void 指针
- 当 int 方法工作正常时,void 方法有何不同,或者为什么我不能调用 void 方法?
- void f()=0是一个虚拟方法
- 将 C++ 对象强制转换为 void* 的安全/标准方法
- 从方法返回 void 以防止执行