I have class containing const fields which require initialisation using a function. Is it appropriate to use a static method of the class to initialise these values in the initialiser list of the constructor?
I have yet to come across a problem doing so, but as I read into the 'static initialisation fiasco' I'm concerned that I'm overlooking something which will come back to bite me later, and either way I'd rather get into the habit of initialising correctly.
Example:
square.hpp:
class Square { const double area; static initArea(double length); Square(double length); } square.cpp
Square::initArea(double length) { return (length * length); } Square::Square(double length) : area(initArea(length)) { return; } Obviously I realise in this case you don't need a function to calculate the area, but in practice the function will determine something more complicated.
double, remove thereturnfrom the constructor and usepublic:specifier where appropriate.initAreaa free static function in the .cpp file, then it does not "pollute" the class header