Polymorphism: not templated base with template derived class using base pointer

This may have an answer, but I could not find an exact example of what I am trying to do.

Suppose you have some kind of common variant-like class (irrelevant data is omitted):

/* 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 */
}

For reasons specific to the project, this type of variant will be related to containers representing collections of the same type of variant. That is, void * data in some Derived will store clearly defined types, and these types may eventually lead to another Derived variant and not know that in advance (this is not possible at compile time) the data must be invalid *.

It is acceptable that Base will track T if necessary, and it can be set as an instance constant in Derived constructors.

, , Derived ( *) 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?

, vtable ( void * Base *), afaik Derived < 1 > :: Read(), , ( ) , , ...

0
1

vtable Base ( ), , Read .

+1

Source: https://habr.com/ru/post/1626066/


All Articles