C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static template

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
vphom
Guest





PostPosted: Fri Aug 22, 2003 4:26 pm    Post subject: static template Reply with 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<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





PostPosted: Sat Aug 23, 2003 8:10 am    Post subject: Re: static template Reply with quote



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 Smile).
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





PostPosted: Sun Aug 24, 2003 11:23 pm    Post subject: Re: static template Reply with quote



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





PostPosted: Tue Aug 26, 2003 8:26 am    Post subject: Re: static template Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.