Boost logo

Boost :

From: Jason Hise (chaos_at_[hidden])
Date: 2005-05-06 09:56:38


Neal Becker wrote:

>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.
>
That's strange, your program works fine for me. The constructor should
be being run on the line where you first create the pointer. If the
constructor isn't being run, you should be getting some sort of runtime
error when you try to dereference the pointer. What compiler are you
using? Can you put a breakpoint inside the constructor to verify that
it is never reached? Are you using the most recent version of the library?

-Jason


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