| View previous topic :: View next topic |
| Author |
Message |
vphom Guest
|
Posted: Fri Aug 22, 2003 4:26 pm Post subject: static template |
|
|
I'm trying to make template for singleton. But I have problem
initialize the static pointer.
in .h file:
template<typename Type>
class Singleton
{
public:
static Type* theItem()
{
if(!item)
{
item = new Type;
}
return item;
}
private:
Singleton();
~Singleton();
Singleton(const Singleton&){}
static Type* item;
};
In cpp file:
template<typename Type>
Type* Singleton::item =0;
doesn't work.
How can I initialize static template type?
vanh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Razvan Cojocaru Guest
|
Posted: Sat Aug 23, 2003 8:10 am Post subject: Re: static template |
|
|
| Quote: | In cpp file:
template
Type* Singleton::item =0;
doesn't work.
How can I initialize static template type?
|
..cpp files and templates don't mix (well you know what I mean ).
Write that in the .h file too.
Regards,
Razvan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Graeme Prentice Guest
|
Posted: Sun Aug 24, 2003 11:23 pm Post subject: Re: static template |
|
|
On 22 Aug 2003 12:26:42 -0400, vphom <vphomsavanh (AT) sbcglobal (DOT) net> wrote:
| Quote: | I'm trying to make template for singleton. But I have problem
initialize the static pointer.
in .h file:
template<typename Type
class Singleton
{
public:
static Type* theItem()
{
if(!item)
{
item = new Type;
}
return item;
}
private:
Singleton();
~Singleton();
Singleton(const Singleton&){}
static Type* item;
};
In cpp file:
template
Type* Singleton::item =0;
doesn't work.
How can I initialize static template type?
vanh
|
template
Type* Singleton<Type>::item =0;
Graeme
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
AH Guest
|
Posted: Tue Aug 26, 2003 8:26 am Post subject: Re: static template |
|
|
Try this small,simple and smart Singleton class
template<typename T>
class Singleton
{
Singleton();
Singleton(const Singleton<T>&);
public:
static T* instance()
{
static T holder;
return &holder;
}
};
Adnan.
vphom <vphomsavanh (AT) sbcglobal (DOT) net> wrote
| Quote: | I'm trying to make template for singleton. But I have problem
initialize the static pointer.
in .h file:
template
class Singleton
{
public:
static Type* theItem()
{
if(!item)
{
item = new Type;
}
return item;
}
private:
Singleton();
~Singleton();
Singleton(const Singleton&){}
static Type* item;
};
In cpp file:
template
Type* Singleton::item =0;
doesn't work.
How can I initialize static template type?
vanh
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|