Sign in to follow this  
thedodgeruk

problem with templates and class constructors?

Recommended Posts

i can get this to work




#include "ttt.h"
[code]

template <class T>

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

return tempModel;

} //


CreateInstanceCModel(ttt( ));[/code]






but i would like a contructor in ttt
but no matter how i code the template i cant get it to work , any ideaas?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Maybe something like this?

[code]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...
[/code]

and then

[code]CModel* model = CreateInstanceCModel<SpecificModel>(arguments);[/code]

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1307659035' post='4821507']
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.
[/quote]
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);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this