Универсальный метод без параметраC++

Программы на C++. Форум разработчиков
Anonymous
Универсальный метод без параметра

Сообщение Anonymous »


I'm attempting to create a generic method that in C# would do the following:

Код: Выделить всё

public class Component{}

public class GameObject{
public T AddComponent() where T : Component{}
}
And I'm trying to convert that to C++ and so far came up with:

Код: Выделить всё

template
Comp& AddComponent()
{
return new Component();  // Work in progress
}
But I don't see how I could call that without passing a parameter. I'm thinking I could use typeof(Comp) where Comp can be any type inheriting Comp. But would there be a way closer to c# where the call ends up something like:

Код: Выделить всё

CompSubType & comp  = AddComponent();


Источник: https://stackoverflow.com/questions/781 ... -parameter

Вернуться в «C++»