Search code examples
c++templates

How to replace template in c++


template<typename ActorType>
ActorType* CreateActor()
{
    EngineActor* NewActor = new ActorType();

    NewActor->SetParent(this); 
    NewActor->Start();
    return dynamic_cast<ActorType*>(NewActor);
}

This is a function that creates and returns a class received as a template.

I want to implement this without using a template. What should I do?

void* CreateActor(int _ActorSize,void* _Dummy)
{
    void* Result = malloc(_ActorSize);
    memcpy_s(Result, _ActorSize, _Dummy, _ActorSize);

    EngineActor* pCast = (EngineActor*)Result;
    pCast->SetParent(this); 
    pCast->Start();

    delete _Dummy;

    return result;
}

I tried a method where the caller creates a dummy, receives the size of the class and the dummy as parameters, and memcpys it. But the problem is that the caller has to use the new operator to create a dummy.


Solution

  • There isn't a way of replacing CreateActor with one function.

    This is a function that creates and returns a class received as a template.

    No it isn't. A function template is not a function, it is a recipe for a family of functions.

    I want to implement this without using a template. What should I do?

    Copy and paste the definition for each different type you use it with.