• Advertisement
Sign in to follow this  

parametrize clas template

This topic is 2406 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello.
I would create a set of classes like:


class Pippo :
public CimporterBase {
public:
Pippo(){};
struct SubType{
static int n1;
};
};

and

class Pluto:
public CimporterBase{
public:
Pluto();
struct SubType{
static int n1;
};
};


and a class:

template <typename T>
class MyClass {
public:

MyClass(){};

typename T::SubType * ptr;
static int nValue;
};

i set some static vars:


Pippo::SubType::n1 = 1;
Pluto::SubType::n1 = 2;


then i wish configure the MyClass class parametrize with Pippo and Pluto.
MyClass<Pippo> p
or
MyClass<Pluto> p

then
if i do in MyClass<Pippo> p
p.ptr->n1// is 1

if i do in MyClass<Pluto> p
p.ptr->n1// is 2


The problem is that i must set the MyClass in another class,
pApi->ImportEntityDS(&nEntityID, &nEntityType);//how i send an instance of MyClass to the function and get the parametrized MyClass?

in the function i think to do:

MyClass<Pippo> or MyClass<Pluto>

but it doesn't do it.
how i can parametrize the MyClass class in the other class andget the correct results?

Thanks.

Share this post


Link to post
Share on other sites
Advertisement
I have a hard time understand what you try to do. You know that MyClass<Pippo> and MyClass<Pluto> are two different types that has nothing in common from the type safety point of view?

Share this post


Link to post
Share on other sites
I'm not 100% sure, is this what you're looking for?

class SomeApi
{
public:
template<typename T>
void ImportEntityDS(EntityId *id, EntityType *type)
{
// Use MyClass<T>
}
};

// Somewhere
pApi->ImportEntityDS<Pluto>(&plutoId, &plutoType);

// Elsewhere
pApi->ImportEntityDS<Pippo>(&pippoId, &pippoType)

Share this post


Link to post
Share on other sites
[quote name='rip-off' timestamp='1311343329' post='4838903']
I'm not 100% sure, is this what you're looking for?

class SomeApi
{
public:
template<typename T>
void ImportEntityDS(EntityId *id, EntityType *type)
{
// Use MyClass<T>
}
};

// Somewhere
pApi->ImportEntityDS<Pluto>(&plutoId, &plutoType);

// Elsewhere
pApi->ImportEntityDS<Pippo>(&pippoId, &pippoType)


thanks rip.-off but that is not the problem, is a text file importer and i have:

template<typename T>
T ImportEntityDS(EntityId *id, EntityType *type)
{
while(!eof)
if(currentline.contains(1) // i find the key 1 in the file
T = myclass<pippo>;
if(currentline.contains(12) // i find the key 12 in the file
T = myclass<pluto>;
return T;??????

i must parametrize myclass inside this function and return filled myclass
}

Share this post


Link to post
Share on other sites
Templates are a compile time mechansim. You cannot make a compile time decision based on the runtime contents of a file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement