Re: Query: want a function that is called at initialisation time
tropostropos@hotmail.com wrote:
Then I still have a problem. I am designing this for a large shop
(30+ developers, some in India) who will be routinely adding new
Singletons to a large body of software. I am making the interface as
simple as possible. The developer just has to derive their new class
MyClass like this:
class MyClass: public Singleton<MyClass> { friend class
Singleton<MyClass>; . . . };
Is there any way I can avoid having to tell them, "by the way, if your
singleton class is called MyClass, you have to add the following line
somewhere in your code:
int dummy = Singleton<MyClass>::initialise(); "
Because, sure as eggs is eggs, some of them will forget to add the
line.
If you reread my example in the previous post, you will see that I never
explicitly referenced 'Singleton<double>::do_initialise()' anywhere in
the code.
So, you could just do as I did: Have 'dummy' be a static member of
Singleton<T>, together with 'initialise()'.
Then you can just add the definition (outside the class) to the file
like this:
template <class T> int Singleton<T>::dummy = initialise();
As long as you make sure that dummy is referenced somewhere in code that
must always be used (like the Singleton<T> constructor, or the
get_instance() function), there are no additional requirements on your
coworkers.
Thanks,
Tropos
Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]