Re: Query: want a function that is called at initialisation time
tropostropos@hotmail.com wrote:
On 13 Dec, 03:52, "Victor Bazarov" <v.Abaza...@comAcast.net> wrote:
Not only it serves no real purpose except to invoke the 'initialise'
member, but I am not sure it's going to actually be instantiated
(it's static member of a template), unless you actually use it.
<snip>
Fair enough. I didn't show all the code. Suppose additionally that
the class has another static function f1 and there is some code
somewhere that calls it:
Singleton<MyClass>::f1( );
I believe you're missing the point. If your static _data_ member
is not used in the program (and by calling it 'dummy' you basically
admit that it's not needed for anything else), then the _data_
member is not going to be instantiated. Hence it's never going to
be initialised, hence your function that you use in the expression
to initialise the 'dummy' member is not going to be called.
So that guarantees that the class will be instantiated, does it not?
Not unless in that 'f1' function you use 'dummy' in some way.
and that dummy will be initialised. So the question remains: what's
the best way to run initialise?
You need to make sure that it initialises something. If 'MyClass'
is the template argument you're going to use without a doubt, then
make your function return 'int' and initialise a non-template int
somewhere in the global scope:
int dummy = Singleton<MyClass>::initialise();
int main() { ...
It's OK to return a boilerplate 0 from it instead of 'void', I think.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]