Topic: static variable in friend function + priva


Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1998/03/01
Raw View
clamage@Eng.sun.com (Steve Clamage) writes:

|>  In article 1@ha2.rdc1.md.home.com, "Tom McKearney" <no@spam.com> writes:
|>  >
|>  >I am trying to do the following (a simple singleton from More Effective C++)
|>  >
|>  >class foo
|>  >{
|>  >    friend foo& theFoo();
|>  >private:
|>  >    foo();
|>  >    ~foo();  // why is this an error?
|>  >};
|>  >
|>  >foo& theFoo()
|>  >{
|>  >    static foo _theFoo;
|>  >    return _theFoo;
|>  >}
|>  >
|>  >It seems to me that, since theFoo() is a friend function, it should be ok
|>  >for the destructor to be private along with the constructor.
|>
|>  Local static variable _theFoo is destroyed at program end, not in
|>  function theFoo. If the destructor is not publicly accessible, the
|>  code is in error.

Paragraph 10 of section 12.4 would seem to imply that the access
control for the destructor of a static object should take place in the
context of the object declaration.  In this case, that would mean that
the above was legal.

Of course, the exact text in this paragraph was only recently changed to
clarify this issue, so it is possible that many, if not most, compilers
do not yet implement it correctly.  (The earlier text said that access
control would be effectuated, but didn't specify the context of the
access control.)

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique orient   e objet --
              -- Beratung in objektorientierter Datenverarbeitung
---
[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: clamage@Eng.sun.com (Steve Clamage)
Date: 1998/02/13
Raw View
In article 1@ha2.rdc1.md.home.com, "Tom McKearney" <no@spam.com> writes:
>
>I am trying to do the following (a simple singleton from More Effective C++)
>
>class foo
>{
>    friend foo& theFoo();
>private:
>    foo();
>    ~foo();  // why is this an error?
>};
>
>foo& theFoo()
>{
>    static foo _theFoo;
>    return _theFoo;
>}
>
>It seems to me that, since theFoo() is a friend function, it should be ok
>for the destructor to be private along with the constructor.

Local static variable _theFoo is destroyed at program end, not in
function theFoo. If the destructor is not publicly accessible, the
code is in error.

If you put _theFoo on the heap, you could choose not to destroy it,
or to destroy it from another friend function (perhaps registered with
atexit()), and still have a private destructor. But unless you have
some reason for making the destructor private, the simplest solution
is to make it public.

Items 26 and 27 in "More Effective C++" have more on these topics.

---
Steve Clamage, stephen.clamage@sun.com



[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]