U
Urs Thuermann
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'd like to hear some thoughts on this.
urs
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'd like to hear some thoughts on this.
urs