In C++, constructors must initialize const variables using initialization lists.
What if my constructor needs to compute the values of those fields? Say via a database lookup or simple computation.
The factory pattern could be applied here, but it seems a bit heavy. I’m considering static methods like X::GetX(param1, param2) that will compute the values and call a private constructor.
Is there a better or more popular pattern that could be used here?
No need for calling a private constructor, you can call static methods (or in some cases, even non-static ones) directly from the initializer list. For example: