Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

private dtor, static instance

0 views
Skip to first unread message

Michael R Cook

unread,
Oct 16, 1997, 3:00:00 AM10/16/97
to

If my class's destructor is private, does that mean I'm not allowed
to create static instances of that of that class?

For example:

class C {
static C& only();
C();
~C();
};
C& C::only()
{
static C c; // error?
return c;
}

The dwp (Nov96) says:

12.4 Destructors [class.dtor]
10 Destructors are invoked implicitly (1) for a constructed object
with static storage duration (_basic.stc.static_) at program
termination (_basic.start.term_), (2) [...]. A program is
ill-formed if the destructor for an object is implicitly used and
it is not accessible (_class.access_).

But it doesn't say what context is used to determine the
accessibility of the destructor that would be invoked at program
termination. I would expect that the construction context would be
used. (Microsoft agrees. Gcc disagrees.)
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std...@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++...@ncar.ucar.edu ]

J. Kanze

unread,
Oct 17, 1997, 3:00:00 AM10/17/97
to

Michael R Cook <michael_cook%era...@cognex.com> writes:

|> If my class's destructor is private, does that mean I'm not allowed
|> to create static instances of that of that class?
|>
|> For example:
|>
|> class C {
|> static C& only();
|> C();
|> ~C();
|> };
|> C& C::only()
|> {
|> static C c; // error?
|> return c;
|> }
|>
|> The dwp (Nov96) says:
|>
|> 12.4 Destructors [class.dtor]
|> 10 Destructors are invoked implicitly (1) for a constructed object
|> with static storage duration (_basic.stc.static_) at program
|> termination (_basic.start.term_), (2) [...]. A program is
|> ill-formed if the destructor for an object is implicitly used and
|> it is not accessible (_class.access_).
|>
|> But it doesn't say what context is used to determine the
|> accessibility of the destructor that would be invoked at program
|> termination. I would expect that the construction context would be
|> used. (Microsoft agrees. Gcc disagrees.)

This has been clarified since the CD2. The context for the
accessibility check of the destructor is the same as the declaration, so
the above code is legal.

Since this clarification is *very* recent, don't expect all compilers to
implement it yet.

--
James Kanze +33 (0)1 39 23 84 71 mailto: ka...@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
I'm looking for a job -- Je recherche du travail

0 new messages