简体   繁体   中英

Method signature to cast void* to a pointer of specific type

Consider the following scenario:

enum Types { NONE, TYPE_A, TYPE_B, TYPE_C }
struct TypeA { int i; double d; };
struct TypeB { int i; float f; };
struct TypeC { double d; };

All the struct s above are unrelated to each other.

Then there are several methods (of different signatures) that fill a vector of any of the struct s above as void* , for instance:

void GetTypeInstances (std:vector<void*>& typeInstances)
{
    void* inst;
    // some logic to create/fetch `struct` instances as void*
    typeInstances.push_back(inst);
}

void GetTypeInstancesAgain (std:vector<void*>& typeInstances, bool flag)
{
    void* inst;
    // some logic to create/fetch `struct` instances as void* using 'flag'
    typeInstances.push_back(inst);
}

In main() , I wish to do the following:

int main()
{
    std:vector<void*> typeInstances;
    GetTypeInstances(typeInstances); // fill the vector

    // depending upon the enum type I want to cast the members of the vector
    for (auto i = 0; i < typeInstances.size(); ++i)
    {
        switch(GetType()) // Types GetType(); is the signature
        {
        case TYPE_A:
            auto* inst = static_cast<TypeA*>(typeInstances[i]);
            // access 'inst' members
            break;
        case TYPE_B:
            auto* inst = static_cast<TypeB*>(typeInstances[i]);
            // access 'inst' members
            break;
        case TYPE_C:
            auto* inst = static_cast<TypeC*>(typeInstances[i]);
            // access 'inst' members
            break;
        }
    }

    // ------------------------------------------------------------

    // Using another method to fetch the vector and access the instances
    std:vector<void*> typeInstancesAgain;
    GetTypeInstancesAgain (typeInstancesAgain, false); // fill the vector

    // depending upon the enum type I want to cast the members of the vector
    for (auto i = 0; i < typeInstancesAgain.size(); ++i)
    {
        switch(GetType()) // Types GetType(); is the signature
        {
        case TYPE_A:
            auto* inst = static_cast<TypeA*>(typeInstancesAgain[i]);
            // access 'inst' members
            break;
        case TYPE_B:
            auto* inst = static_cast<TypeB*>(typeInstancesAgain[i]);
            // access 'inst' members
            break;
        case TYPE_C:
            auto* inst = static_cast<TypeC*>(typeInstancesAgain[i]);
            // access 'inst' members
            break;
        }
    }

    // -----------------------------------------------------

    // Repeating the above for several other methods

    return 0;
}

Q1) Is there a way to extract the switch to a method that casts the void* to their respective types? I want to remove code duplicacy. Note that the enum contains many more types, and there are many more corresponding struct s for those types.

I am thinking of a method, but since the struct s have no relation among themselves, I am not sure how to go about solving this:

????  Cast(int enumType, void* ptr)
{
    ????
}

Q2) Will this problem be any simpler if the vector is filled with instances of a specific type only? If so, what would be it?

All possible leads are appreciated.

If you want to "// access 'inst' members" then you are more-or-less forced to write some code to do that in each case.

The obvious OO way round that would be to have a base class, and a pure virtual accesinstmembers() function, obviously named something more sensible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM