Setting a constant at compile time

Hey there!

I have a class of which the constructor receives an I2C address as an argument. Now what I want to do is save that argument in a constant variable so that it cannot be changed anymore. Does anyone have an idea on how to achieve that? I've read a little bit into c++ constexpr but I do not see how that can be applied to variables rather than fuctions.

Greetings
V

Use an initializer list. See Case #1 Here.

EDIT:
Note that the value isn't assigned to the constant at "compile time" but rather when the object is constructed. This is what you really want as the object could be created dynamically with the 'new' operator.

1 Like