In the following C++ code:
struct Features {
int F1;
int F2;
int F3;
int F4;
Features(int F1,int F2,int F3,int F4)
: F1(F1), F2(F2), F3(F3), F4(F4) { }
};
What does this part mean?
Features(int F1,int F2,int F3,int F4)
: F1(F1), F2(F2), F3(F3), F4(F4) { }
Thanks.
It is initializing the member variables using the constructor’s initializer list. it would be clearer if the names of the constructor parameters weren’t the same as the data members:
It is useful to have some naming convention for data members, such that these are easily identifiable and distinguishable from local variables in code. Examples are prefixing an
m_or using a trailing_:Both these constructors can be used like this:
Note that the fact that this constructor has been defined means that the compiler will no longer provide a default constructor. So if you want to be able to to this:
then you need to provide your own default constructor: