I have a fairly simple const struct in some C code that simply holds a few pointers and would like to initialize it statically if possible. Can I and, if so, how?
No, a struct is a class where members and bases are public by default. Structs can still have private members.
In C++ the const keyword has been improved and you can declare real const values. That's why it has to be initialized before compiling the code. The rule is slightly different: to be used in an integral constant expression, the initialization must be visible to the compiler.
You can, if the pointers point to global objects:
// In global scope
int x, y;
const struct {int *px, *py; } s = {&x, &y};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With