It is clear that a data member of a class declared as static
is incorrect to be initialized in ctor initializer list (because it "is not a member" of an object). But it works fine if it is a protected
member. Why?
// foo.h
class Foo {
public:
explicit Foo();
~Foo() = default;
protected:
static int kProtected;
private:
static int kPrivate;
}
// foo.cpp
Foo::Foo()
: kProtected(1), // OK (?!)
kPrivate(1) {} // error C2438: 'kPrivate': cannot initialize static class data via constructor
Aucun commentaire:
Enregistrer un commentaire