const member initialisation

Hi guys,
suppose I have a set of instance variables declared as const double, I would like to initialise them in the constructor.
Besides of initalising in the ctor list (e.g. MyClass() : my_const_var(0), my_const_var2(init_var2()), etc.. { }), is there any way to set their value inside the constructor body ?

regards
-s.fox
Edit: Ignore me - didn't spot that the OP specified const.
Last edited on
They have to be initialized in the ctor init list. After that you can't give them a value because they are const.
No, inside the constructor body you may not iniialize them because there is used an assignment instead initialization.
However you can declare them as constexpr static variables and assign them values in the class definition. For example

1
2
3
4
struct A
{
   static constexpr double x = 1.0;
};
oky thanks guys
Topic archived. No new replies allowed.