c++ - Pass in-class initialized const member to Base constructor? -



c++ - Pass in-class initialized const member to Base constructor? -

i have in-class initialized const fellow member in derived class i'd pass constructor of base of operations class.

example:

class base{ public: base(int a) : i(a){} private: int i; }; class derived : base{ public: derived() : base(a){} private: const int = 7; }; int main(){ derived d; }

however spawns uninitialized error:

field 'a' uninitialized when used here [-wuninitialized]

i under impression const initializing set value straight allowing passed derived ctor in manner. doing wrong or under wrong impression? when const in-class initialized members initialized?

your question,

when const in-class initialized members initialized?

is bit of reddish herring. "in-class initialized" doesn't mean anything; brace-or-equal initializer syntactic sugar , takes place of corresponding constructor initalizer list slot. const has no special bearing. real question should be:

when non-static info members initialized?

the details don't matter much, suffice non-static info members initialized after base of operations subobjects initialized, proposed construction cannot work.

the straight-forward reply not utilize brace-or-equals-initializer , utilize normal (possibly defaulted) constructor parameter. here few examples:

struct foo : base of operations { const int a; // default constructor, mention value 1 time foo(int _a = 10) : base(_a), a(_a) {} // dryolent default constructor foo() : base(10), a(10) {} // delegating default constructor foo() : foo(10) {} private: foo(int _a) : base(_a), a(_a) {} };

alternatively, if value of constant doesn't need configurable, can create per-class constant (rather per-object):

struct foo : base of operations { static const int = 10; foo() : base(a) {} }; const int foo::a; // if odr-use foo::a

c++ c++11

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -