J
jedi200581
Hello,
I have a singleton class that I'm using through macros:
//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \
class MyClass
{
public:
MyClass() {}
static MyClass *getInstance ()
{
static MyClass *myObject = new MyClass();
return myObject;
}
void doSomething(MyArgument myArgument) { whatever(); }
};
// snippet
Now, I would like to template MyClass by another class, and don't
know how to do that! I guess I need some kind of initialization macro
where I give the template class to use, but how getInstance will
remember it for the DO_SOMETHING call ? Is there a way to store a
class in a variable or something ?
This code here:
//Snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass<TemplateClass> *myObject =
MyClass<TemplateClass>::getInstance(); \
myObject->doSomething(myArgument); \
}; \
template <class T>
class MyClass
{
public:
MyClass() { _t = new T(); }
static MyClass *getInstance ()
{
static MyClass<T> *myObject = new MyClass<T>();
return myObject;
}
void doSomething(MyArgument myArgument) { _t->whatever(); }
T *_t;
};
// snippet
Obviously doesn't work, but can help you understand my pb. Is there
an elegant solution ?
Thank you.
I have a singleton class that I'm using through macros:
//snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass *myObject = MyClass::getInstance(); \
myObject->doSomething(myArgument); \
}; \
class MyClass
{
public:
MyClass() {}
static MyClass *getInstance ()
{
static MyClass *myObject = new MyClass();
return myObject;
}
void doSomething(MyArgument myArgument) { whatever(); }
};
// snippet
Now, I would like to template MyClass by another class, and don't
know how to do that! I guess I need some kind of initialization macro
where I give the template class to use, but how getInstance will
remember it for the DO_SOMETHING call ? Is there a way to store a
class in a variable or something ?
This code here:
//Snippet
#define DO_SOMETHING ( myArgument) \
{ \
MyClass<TemplateClass> *myObject =
MyClass<TemplateClass>::getInstance(); \
myObject->doSomething(myArgument); \
}; \
template <class T>
class MyClass
{
public:
MyClass() { _t = new T(); }
static MyClass *getInstance ()
{
static MyClass<T> *myObject = new MyClass<T>();
return myObject;
}
void doSomething(MyArgument myArgument) { _t->whatever(); }
T *_t;
};
// snippet
Obviously doesn't work, but can help you understand my pb. Is there
an elegant solution ?
Thank you.