Универсальный метод без параметра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
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

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