My question must be simple, but I cannot find a right way to split the constructor with initialized members to .h and .cpp (definition and implementation), files.
If, say, I have:
class Class {
public:
Class (int num, float fl ... ) : _num(num), _fl(fl) {}
...
private:
int _num;
float _fl;
};
How do I implement the constructor in .cpp file? If, after member initialization (in header file) I put semi-colon - compiler complains. If I keep { }, the .cpp file will the re-define the constructor, which is also wrong. I can, of course, write down the definition explicitly. So that the .h file is:
Class (int num, float fl ... );
and .cpp :
Class (int num, float fl ... ) {
_num = num;
_fl = fl;
}
But is there a way to keep the member initialization in the header file? Thanks in advance! Also, in professional coding, what would be preferred?
You need to have members init and construction implementation in the .cpp
Class::Class (int num, float fl ... ):_num(num),_fl(fl)
{
// whatevever other init task you want to do goes here
}