多态性:非模板化的基类与模板化的派生类,使用基指针
polymorphism: non-templated base with templated derived class, using base pointer
这可能已经有一个答案,但我还没有能够找到一个确切的情况下,我正在尝试。
假设您有一些类似于这样的泛型变体类(不相关的细节省略):
/* in hpp file */
struct Base {
void * data;
Base();
virtual ~Base();
// ...
virtual bool Read(...);
}
template <Some_Enum_Class T>
struct Derived : Base {
// T-specific interface to deal with Base::data
bool Read(...); /* implementation in separate cpp file */
}
由于特定于项目的原因,这样的变体类型引用表示该相同变体类型的集合的容器是可能的。也就是说,某些Derived中的void*数据将存储定义良好的类型,这些类型最终可能会导致派生的另一个变体,并且不知道哪些数据必须是void*(在编译期间不可能)。
如果必要的话,Base可以跟踪T,并且可以将其设置为派生构造函数中的实例常量。
我不确定的是,在我创建一个派生的实例并将其存储(作为Base*)在void*中会发生什么。
如果我采取void*并将其转换为Base*(因为我将无法从运行时获取类型信息,也不想超出这里已经发生的事情),调用函数(如Read)是否会正确使用派生版本,尽管编译器无法确定T是什么?
在代码:Derived <1> * D = new Derived <1>;
Base * B = (Base*) D; // c-cast for brevity unless this is part of the answer
SendAsVoidPtr( (void*) B ); // at this point the type information is lost
之后
void * arg = ReceiveVoidPtr();
Base * B = (Base*) arg;
Base->Read(...); // which version does this call and why?
我猜(希望)虚函数表只依赖于地址(作为void*或Base*),所以这应该可以正常工作,并调用Derived<1>::Read()函数,尽管编译器(可能)无法提前确定类型,但我想确定在此结构之上构建之前…
指向vtable
的指针本质上是Base
中隐藏的实例成员(在大多数实现中),所以是的,您将能够调用Read
和其他虚函数的正确覆盖
相关文章:
- 如何使用基类指针引用派生类成员
- 使用基类指针创建对象时,缺少派生类析构函数
- 如果基类包含双指针成员,则派生类的构造函数
- 使用基类指针调用基类的值构造函数的语法是什么?
- 如何在基类指针向量的元素上应用重载的多态函数
- 如何使用 std::make_shared 创建基类类型的智能指针?
- 如何在不使用指针的情况下将派生类的对象作为参数传递给基类中的函数?
- 指向基类派生类的 std::unique_ptr 的指针
- 当目标指针不是基类的类型时,为什么允许dynamic_cast为多态类生成 null 指针?
- 如何在成为指向基类的指针后保留对子类方法的使用?
- 派生类是从基类继承 v 指针并仅使用它,还是也有自己的 v 指针?
- std::variant vs指向C++中异构容器基类的指针
- 模板化函数或具有指向基类的指针的函数
- 指向基类的唯一指针
- C++函数解析基类指针到派生类指针
- 指向基类的指针的 std::vector 的深层副本
- 在将派生类指针类型转换为派生类指针后,从基类指针调用派生类函数
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 无法从指针派生类转换为指针基类(多态性)
- 使用指针基类的=操作符的多态性