c++ - Set struct elements to values within definition -
the compiler seems have no problem this. can safely assume object create of type have these defaults values?
struct colorproperties { bool colorred = true; bool colorblue = false; bool isrectangle = true; }; colorproperties myproperties;
will myproperties
automatically contain element values noted struct?
yes, can. it's c++11 feature. it's equal to
struct colorproperties { colorproperties() : colorred(true), colorblue(false), isrectangle(true) {} // };
you can read proposal here
quotes standard.
n3376 12.6.2/8
in non-delegating constructor, if given non-static data member or base class not designated mem-initializer-id (including case there no mem-initializer-list because constructor has no ctor-initializer) , entity not virtual base class of abstract class (10.4), then
— if entity non-static data member has brace-or-equal-initializer, entity initialized specified in 8.5;
struct { a(); }; struct b { b(int); }; struct c { c() { } a; const b b; // error: b has no default constructor int i; // ok: has indeterminate value int j = 5; // ok: j has value 5 };
Comments
Post a Comment