c++ - Only static and const variables can be assign to a class? -
i learning c++. curious, can static , constant varibles assigned value within class declaration? why when assign values normal members, have special way doing
void myclass::init() : member1(0), member2(1) { }
this looks supposed constructor; if is, should have no return type, , needs have same name class, e.g.,
myclass::myclass() : member1(0), member2(1) { }
only constructor can have initializer list; can't delegate type of initialization init
function.
any nonstatic members can initialized in constructor initializer list. const , reference members must initialized in constructor initializer list.
all things being equal, you should prefer initialize members in constructor initializer list, rather in body of constructor (sometimes isn't possible or it's clumsy use initializer list, in case, shouldn't use it, obviously).
Comments
Post a Comment