【发布时间】:2020-12-20 16:52:43
【问题描述】:
我的情况是,我需要一个指向未知类型对象的指针作为另一个类的成员。 但是有效对象类型的列表在编译时是已知的。
说我有:
// A class templated on T
template<class T> class obj;
// Now assume there are some objects of types obj<...> somewhere else
obj<int> obj1;
obj<double> obj2;
// Now, manager needs to create a pointer for these objects
// in a specific manner at construction-time
// Say, we have to choose a canonical object (and set the other as secondary)
// manager shouldn't be templated on T because It needs
// to have multiple members of type obj<...>
template<class R>
class manager
{
shared_ptr<...> canObjPtr;
shared_ptr<...> secObjPtr;
public:
// once set, canonical obj is not expected to change
explicit manager(string canonicalObj);
}
我怎样才能做到这一点?
一些最初的想法并没有真正起作用:
-
我能想到的最有前途的方法是将 T1 和 T2 模板参数添加到管理器并构造如下:
manager<R, obj<int>, obj<double>>()。我觉得我应该在构造管理器之前使用静态函数获取“canonicalObj”字符串,然后决定创建哪个管理器manager<R, obj<int>, obj<double>>或manager<R, obj<double>, obj<int>>。 -
直接在
obj1 and obj2对象上“模板化”管理器怎么样。看起来可行吗?请注意,我可以灵活地将模板参数添加到管理器,因为它涉及一些不喜欢使用多参数模板的运行时选择机制。 -
创建 4 个而不是 2 个成员指针(见下文,但这很糟糕,并且肯定会让我在实现中发疯:需要在使用其中任何一个之前始终检查一个指针是否为空)
template<class R> manager
{
sharedPtr<obj<int>> canObjPtrI;
sharedPtr<obj<float>> canObjPtrF;
// same for secObjPtr above
public:
explicit manager(string canonicalObj);
}
-
std::any and std::variant(和它们的 boost 等价物)是不可能的,因为我想继续使用 c++11 并且不能通过策略使用 boost。如果我要打破其中一条规则,我会考虑升级到 c++17。 -
我不认为使用
shared_ptr<void>可以带来任何好处,因为无论如何我都必须将指针转换为正确的类型,并且不能使用 void 指针中的对象接口。 -
union也是如此。与 3 相比,它几乎没有改进。
另外,如果您认为这是一个潜在的设计问题,请不要犹豫。我邀请您指出您注意到的任何缺陷/改进。
[编辑] 这段代码试图做的是......
基本上,我需要从预先构建的obj<int> and obj<double> 对象列表中选择一个规范和次要对象,就像我上面解释的那样:
根据用户输入,该类应决定规范对象并根据此决定执行计算。我已经有工具可以通过它们的名称(字符串)获取对这些对象的引用。唯一的问题是它们的类型不同,让它们从基类继承会限制我只使用该基类的接口(这是正确的吗?)。
cmets 中要求的最小示例
// A base template for objects defines common
// and possibly different interface.
template<class T> class objBase
{
protected:
field<T> f_;
public:
// Public data type
using fieldType = field<T>;
// New selects from "Registered childs of objBase"
// at runtime
static shared_ptr<objBase>::New() const;
// Pure virtual to update the obj
virtual void update() = 0;
// Const-Access to field
const field<T>& getField() const;
};
// Created objects are also registered automatically to a database
// i.e. I can get (const-)refs to them by querying the database
shared_ptr<objBase<int>> obj1 = objBase<int>::New();
shared_ptr<objBase<int>> obj2 = objBase<int>::New();
shared_ptr<objBase<float>> obj3 = objBase<float>::New();
// In manager, something like this needs to happen
template<class R>
class manager
{
private:
// Read 2 target obj names from user input
pair<string,string> objNames;
// Possible types for requested objs:
// obj_a, obj_b : both objBase<int> or both objBase<float>
// or they can have different types
// But we essentially need only:
pointer<...> canonicalObj;
pointer<...> secondaryObj;
// These should be used like this
void useObjField() const
{
// Not using auto for clarity
const decltype(*canonicalObj)::FieldType& objField
= canonicalObj->getField();
for(int i=0; i < objField.size(); ++i)
{
// Loop through elements and use them for some calculations
// Related to other fields in manager
}
}
};
【问题讨论】:
-
我需要一个指向未知类型对象的指针作为另一个类的成员。但是有效对象类型的列表在编译时是已知的。
std::variant确实是我想到的第一个。 -
您的问题必须非常关注您的解决方案(指向某些类型的指针),而不是您要解决的问题。如果您可以描述此代码解决了什么样的问题,我们可以提供更适合该问题的解决方案。现在我们可以批准或批评您的方法。
-
您可以为自己的特定用例实现“刚刚好”
std::variant。 -
好的,你有一个指向未知类型对象的指针。你打算用它做什么?
-
“根据用户输入,类应该决定一个规范对象并根据这个决定执行计算。”——听起来确实是一个适合运行时/动态的问题多态性。从一些公共基础继承
obj<T>有什么问题?
标签: c++ c++11 pointers unions void-pointers