Boost logo

Boost :

From: Pavel Vozenilek (pavel_vozenilek_at_[hidden])
Date: 2005-05-06 09:50:02


"Neal Becker" write:

> I'm a little confused about singleton usage. Can a singleton have a
> constructor?
>
> I tried this:
>
> using boost::basic_singleton;
>
> struct Example :
> public basic_singleton<Example> {
> Example() {
> std::cout << "Hello\n";
> }
> int Cnt() const { return 0; }
> };
>
> int main() {
> Example::pointer ptr;
> std::cout << ptr->Cnt();
> }
>
> ./Test1
> 0
>
> It appears constructor wasn't run, because nothing was printed.
>

I tried it with Intel 7 (plugged in VC6 IDE) and it works for me.
What is your compiler?

Singleton can have constructors, many of them and
with parameters too.

One may do:

Example::pointer ptr;
...
ptr->destroy();
ptr->create(100, "sss", 3.14);
...
ptr->destroy();
ptr->do_something(); // default ctor will be used first

...
ptr->destroy();
ptr->create(); // default ctor
ptr->create(); // ignored here

/Pavel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk