Re: question re. usage of "static" within static member functions of
a class
On Sep 9, 9:27 am, Francesco <entul...@gmail.com> wrote:
On Sep 8, 11:39 pm, James Kanze <james.ka...@gmail.com> wrote:
[...]
And doesn't avoid the order of initialization issues the other
versions were designed to avoid.
Well, to be precise, the last version above isn't even
applicable, because the ctor is private. I've already
straightened all the points above, partly by myself, mostly
with Paavo, as you can see from all the posts between the one
you are quoting from me and the one I'm replying to. I suppose
you didn't receive them. Freakin' NNTP.
I saw that latter. The problem isn't that I don't see articles;
the problem is that I don't see them in the same order. So I
respond to one article before seeing the next.
[...]
Not knowing the requirements, it's hard to say. My usual
implementation is:
class Data
{
private:
static Data* ourInstance ;
Data() {}
~Data() {}
public:
static Data& instance() ;
// ...
} ;
Data* Data:: ourInstance = &instance() ;
Data&
Data::instance()
{
if ( ourInstance == NULL ) {
ourInstance = new Data ;
}
return *ourInstance ;
}
This solves the threading issues (for the most part), and
avoids any order of destruction issues, by not destructing
the object.
Thank you for showing an example of your implementation, it
helps me confronting the different approaches and getting a
better grip on the subject.
Actually, I posted it because I thought it might intregue you.
It does depend on a somewhat subtle feature of C++: the fact
that Data::ourInstance is initialized twice, first to NULL, and
then with the initialization expression.
--
James Kanze