如何确定非多态类型的 mixin 模板的类型兼容性
如果我有一个混合定义为...
template<class T> class Mixin : public T
{
// mixin methods and members
};
...并用 T 声明它是一个非多态类...
Mixin<NonPoly> mixin;
...然后有一个指向它的基类指针...
NonPoly* nonPolyPtr = &mixin;
...以后我该怎么办确保 nonPolyPtr 指向 Mixin 类型?
dynamic_cast<Mixin*>(nonPolyPtr)
上面的代码无法编译,因为基类是非多态的。
我看到 Boost 有一些可能有帮助的特征类,但我希望有一个我忽略的更简单的解决方案。
If I have a mix-in defined as...
template<class T> class Mixin : public T
{
// mixin methods and members
};
...and declare it with T being a non-polymorphic class...
Mixin<NonPoly> mixin;
..and then have a base class pointer to it...
NonPoly* nonPolyPtr = &mixin;
...how can I later ensure nonPolyPtr is pointing to a Mixin type?
dynamic_cast<Mixin*>(nonPolyPtr)
The above does not compile because the base class is non-polymorphic.
I saw Boost has some trait classes that may help, but I'm hoping there's a simpler solution I'm overlooking.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找错误的要求。您不需要在此处进行任何转换,但您可能需要对代码进行一些重组。如果你有类 A 的关系,它创建 mixin 和使用 NonPoly 的 B,那么只需将 NonPoly 指针传递给 B 并直接在 A 中使用 mixin 即可。没有理由为了尝试而放弃 A 中的类型信息再次把它拿回来。如果类多了,分成懂mixin的和懂NonPoly的,也是一样的关系。
如果一开始就是这种情况,那么 mixin 设计很可能不是正确的方法。很多时候,当需要简单的包含时,会使用 mixin。在我上面的 A 和 B 示例中,您可能有一个 Mixin 类
,然后在需要操作该对象时只需传递该对象即可。或者更常见的是,如果您只是将 T 传递给某个第 3 方库,则根本不需要 mixin。遏制甚至可能不是最好的。当您可以通过类型 T 的公共接口和公共第 3 方例程来操作类型 T 时,维护封装的最佳方法始终是编写文件范围算法。
如果您可以解释为什么您认为需要丢失类型信息然后再恢复,我们也许能够更清楚地展示如何重组所有权,这样就不需要发生这种情况,但由于这种类型信息永远不会离开运行时(因为你正在寻找演员 - 你的问题意味着它没有被序列化或任何东西),我可以向你保证,有一些设计,类型信息一开始就不会丢失。
I think you are looking at the wrong requirements. You don't need to do any casting here, but you may need to do some restructuring of your code. If you have a relationship of classes A, which creates mixin and B which uses NonPoly, then just pass B the NonPoly pointer and use the mixin directly in A. There should be no reason to give up the type information in A just to try to get it back again. If there are more classes, separate them into those who know the mixin and those who know NonPoly, and it's the same relationship.
And it is very likely that if this is the case in the first place, a mixin design is not the proper approach. Very often, mixins are used when simple containment is needed. In my example with A and B above, you may have a Mixin class
and then just pass the object when it needs to be operated on. Or even more common, if you are just passing T to some 3rd party library, you need no mixin at all. Containment might not even be best. The best way to maintain encapsulation is always to write file-scope algorithms when you can manipulate the type T through it's public interface and public 3rd party routines.
If you can explain why you think you need to lose the type information and then later recover, we might be able to show more clearly how you can restructure ownership so that doesn't need to happen, but since this type information never leaves the runtime (since you are looking to cast - your question implies it's not getting serialised or anything), I can assure you that there is some design where that type information is not lost in the first place.
如果您确定其类型,只需使用
static_cast
进行向下转换。您还需要在转换Mixin*
中指定模板参数。If you are certain of its type just use
static_cast
to downcast. You also need to specify the template parameter in the castMixin<NonPoly>*
.