problem with templates and class constructors?

Started by
3 comments, last by thedodgeruk 12 years, 10 months ago
i can get this to work




#include "ttt.h"


template <class T>

CModel* CreateInstanceCModel(T()) //
{
CModel* tempModel = new T();
m_modelList.Push_Back(tempModel);

return tempModel;

} //


CreateInstanceCModel(ttt( ));







but i would like a contructor in ttt
but no matter how i code the template i cant get it to work , any ideaas?
Advertisement
What is it you're trying to do? What is the template type supposed to represent? What are you doing with the parameter you pass to the function? Your question is very unclear.
It looks like you're getting confused with c++ templates here. You're creating a type T, which will be replaced by the compiler with the type you specify.
This looks like an attempt to implement a factory function for different types with a common CModel ancestor. How about simply registering a function or class to create a given CModel derivative, and then calling CreateInstanceCModel() to use the factory function and create an instance of that class.

Saving the world, one semi-colon at a time.

Maybe something like this?

template <typename T>
CModel* CreateInstanceCModel()
{
CModel* model = new T();
m_modelList.push_back(model);
return model;
}

template <typename T, typename A1>
CModel* CreateInstanceCModel(A1 const& a1)
{
CModel* model = new T(a1);
m_modelList.push_back(model);
return model;
}

template <typename T, typename A1, typename A2>
CModel* CreateInstanceCModel(A1 const& a1, A2 const& a2)
{
CModel* model = new T(a1, a2);
m_modelList.push_back(model);
return model;
}

// Etc...


and then

CModel* model = CreateInstanceCModel<SpecificModel>(arguments);

What is it you're trying to do? What is the template type supposed to represent? What are you doing with the parameter you pass to the function? Your question is very unclear.

CModel is my base model class

i want to be able to create a class that has a base of CModel

eg Rocket , gun, player, enemy etc

and impliment it with CreateCModel(Rocket());



but would like to use constructors though in the creation off . eg Rocket(CMEsh* meshTypeUsed);

This topic is closed to new replies.

Advertisement