Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #7312

Re: Private static class members

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Private static class members
Date 2011-07-01 10:46 +1200
Message-ID <974cnpFnfiU5@mid.individual.net> (permalink)
References <ygfd3hv7hgq.fsf@janus.isnogud.escape.de>

Show all headers | 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


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