Topic: Scope of a static local object.


Author: Steve Harclerode <harclers@norand.com>
Date: 1996/10/28
Raw View
Bruce Florman wrote:
> I'm reasonably sure that I've run into (yet another) bug in the Microsoft C++
> compiler (v4.2), but before I send off a nastygram I figured that I ought to
> make some queries.
> Are there any "special" requirements on the scope of a static local object?
> Specifically, I've got a class that looks something like this:
>
>         class X
>         {
>         public:
>             static X& Instance ();
>         private:
>             X ();
>             ~X ();
>         };
>
>         X& X::Instance ()
>         {
>             static X theX;
>             return theX;
>         }
>
> The intent here is that X be a singleton class whose only instance is accessed
> through the X::Instance static member function.  Unfortunately the compiler
> claims that the private destructor is inaccessable from X::Instance.  It is
> definitely the static storage duration that it dislikes, since it cheerfully
> accepts code that constructs and destroys automatic and dynamic instances.  I
> can't find anything in the 4/95 DWP that would indicate that the destructor
> would be inaccessable from a member function of X -- even though the actual
> destruction of theX does not overlap the lifetime of any X::Instance invocation.
> Does anyone else believe otherwise?

I tried the same thing on my old Borland compiler (v 4.52), and I received
the same error. It seems odd to me, I can't think of any good reason to
specify different access rules for the destructor in the case of a local
static object.

- Steve Harclerode


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Bruce Florman <bflorman@indy.net>
Date: 1996/10/15
Raw View
I'm reasonably sure that I've run into (yet another) bug in the Microsoft C++
compiler (v4.2), but before I send off a nastygram I figured that I ought to
make some queries.

Are there any "special" requirements on the scope of a static local object?
Specifically, I've got a class that looks something like this:

 class X
 {
 public:
     static X& Instance ();
 private:
     X ();
     ~X ();
 };

 X& X::Instance ()
 {
     static X theX;
     return theX;
 }

The intent here is that X be a singleton class whose only instance is accessed
through the X::Instance static member function.  Unfortunately the compiler
claims that the private destructor is inaccessable from X::Instance.  It is
definitely the static storage duration that it dislikes, since it cheerfully
accepts code that constructs and destroys automatic and dynamic instances.  I
can't find anything in the 4/95 DWP that would indicate that the destructor
would be inaccessable from a member function of X -- even though the actual
destruction of theX does not overlap the lifetime of any X::Instance invocation.
Does anyone else believe otherwise?

--Bruce Florman


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]