class - C++ Private structure and non static const variables initialization -
i totally disappointed. have class, there have private structure. , what's silly problem: can't preinitialize variables!
mean:
need:
struct somestruct { somestruct *next = null; int number; };
i want create easy dynamic list, adding new elements heap.
, should do?
struct somestruct { somestruct *next; int number; };
put
somestruct *newelement = new somestruct; newelement.next = null;
every time? can forget this.
please me. because it's not problem when need add 1 useless string. if have 50 default variables?
you cannot initialize class members when declare them. instead, need constructor:
struct somestruct { somestruct(): next(null), number(0) {} somestruct *next; int number; };
alternatively, pod (plain old data), use original class no constructors , use somestruct *newelement = new somestruct();
create somestruct
. initializes members zero.
also, c++11 supports in-class member initializers.
Comments
Post a Comment