Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #7312
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Ian Collins <ian-news@hotmail.com> |
| Newsgroups | comp.lang.c++ |
| Subject | Re: Private static class members |
| Date | Fri, 01 Jul 2011 10:46:48 +1200 |
| Lines | 54 |
| Message-ID | <974cnpFnfiU5@mid.individual.net> (permalink) |
| References | <ygfd3hv7hgq.fsf@janus.isnogud.escape.de> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net Z/afMqG+9vBxy4ZBiXfbKQB4BnOULP2xnh/LS60ucVsvQHgpvx |
| Cancel-Lock | sha1:b7UoVqom+ZUWa9Xx/1U2CO3uUbU= |
| User-Agent | Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:1.9.2.9) Gecko/20101021 Lightning/1.0b2 Thunderbird/3.1.4 |
| In-Reply-To | <ygfd3hv7hgq.fsf@janus.isnogud.escape.de> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.c++:7312 |
Show key headers only | View raw
On 06/30/11 10:15 PM, Urs Thuermann wrote:
> Is it wise to use private static class members in a header file? Say
> I have a class that wants to keep a list of pointers to all created
> objects:
>
> ---- foo.h ----
> class Foo {
> public:
> Foo();
> ~Foo();
> private:
> static std::list<Foo *> all_foos;
> };
> ---- foo.cc ---
> std::list<Foo *> Foo::all_foos;
>
> Foo::Foo() {
> ...;
> all_foos.push_back(this);
> }
> Foo::~Foo() {
> all_foos.remove(this);
> ...
> }
> ---------------
>
> Having this in a header file exposes implementation details to the
> user and causes unnecessary recompilation of all files using this
> header when I change (say I rename) that private static member.
>
> As long as I have no inline member functions using the list, I would
> tend to move the list `all_foos' from the class by removing it from
> foo.h completely and removing the Foo:: in foo.cc and making it static
> there, i.e. in foo.cc
>
> static std::list<Foo *> all_foos;
>
> OTOH, this means that `all_foos' is no longer part of the class and
> may be accessilbe by other non-member and non-friend functions in
> foo.cc.
I find myself doing this fairly often. If the types and instances
aren't exposed by the classes public interface, I tend to place them all
in the unnamed namespace (I use this rather than static because I group
all local classes, type names and variables together).
One example I have is a name service information cache. The cache
comprises a number of maps and sets which are private to the implementation.
An alternative approach if the are private types is to place them in
their own headers, which facilitates unit testing.
--
Ian Collins
Back to comp.lang.c++ | Previous | Next — Previous in thread | Find similar | Unroll thread
Private static class members Urs Thuermann <urs@isnogud.escape.de> - 2011-06-30 12:15 +0200
Re: Private static class members Juha Nieminen <nospam@thanks.invalid> - 2011-06-30 10:54 +0000
Re: Private static class members Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-30 08:16 -0400
Re: Private static class members Juha Nieminen <nospam@thanks.invalid> - 2011-06-30 12:41 +0000
Re: Private static class members Victor Bazarov <v.bazarov@comcast.invalid> - 2011-06-30 13:26 -0400
Re: Private static class members Paul Brettschneider <paul.brettschneider@yahoo.fr> - 2011-07-01 00:32 +0200
Re: Private static class members red floyd <redfloyd@gmail.com> - 2011-07-01 15:18 -0700
Re: Private static class members Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-06-30 15:17 +0000
Re: Private static class members Ian Collins <ian-news@hotmail.com> - 2011-07-01 10:46 +1200
csiph-web