Re: Problem with Singleton template (2)
Hi best
Thanks very much for your reply. You were right, the compiler error
was just
my mis-typing. Sorry about that but I really appreciated your answer.
As you
predicted, my implementation failed because of the need to up-cast the
instance
pointer. So I am now trying to implement your suggestion. Here's an
outline of
my current code:
template <typename T> class Singleton
{
public:
static T* instance()
{
if (mp_instance == 0)
mp_instance = new T; <<========= Line 48
return mp_instance;
}
...
private:
static T* mp_instance;
};
template <class T> class MyList
{
friend class Singleton<MyList>;
....
}
class IntList : public MyList<int> { }; <<======= Line 26
typedef Singleton< IntList > theIntList;
main()
{
theIntList* ptheIntList = theIntList::instance(); <<======= Line
31
int length = ptheIntList->getListLength(0,0);
}
Now I get the following compiler errors:
line 26: error: "MyList<T>::MyList() [with T=int]" is inaccessible
detected during:
implicit generation of "IntList::IntList()" at line 48 of
"Singleton.h"
instantiation of "T *Singleton<T>::instance() [with T=IntList]"
at line 31
line 31: error: a value of type "IntList *" cannot be used to
initialize an entity of type "theIntList *"
Please can you suggest a fix for the errors? I can't see what is
wrong.
Do I need to make Singleton a friend of MyList as shown above?
Thanks again for your help.
David
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]