C++ Where to put default initialization values that are reused?

I have a several constructors, each of which take many default arguments. Instead of hardcoding the default argument values, I want to make them variables. So I have code like the following:

const int var1{ 1};
const int var2{ 1};

class MyClass
    public(const int my_var = var1, const int second_var = var2);

My question is: Where is the best place to put the default variables as I do not want to hard code them into the constructor parameters?

Should they be a member of MyClass?

Should I just globally declare them with static at the top of the file?

1 answer

  • answered 2018-01-14 11:31 user8552182

    I don't think the default arguments in a members function can be variables. The usual practise would be to create them as members variables and then just assign them in the constructor.