Then you have to write C.
C++ is not C so C++ code often does not translate to C. Also, some C syntax and rules are C++ compile errors. You can't have it both ways.
However, the C solution is:
-
typedef struct {
-
int Start;
-
int attribute;
-
} MyStructure ;
-
-
#define FIRST 1
-
-
-
-
int main()
-
{
-
const MyStructure STRUCT0={FIRST, 0} ;
-
const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
-
const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
-
-
}
-
You have to initialize your struct variables inside main().
Doing it outside of main() requires a constructor, which is not present in C. Since you don't have a constructor, the compiler provides one that initializes each member of the struct using the values between the braces. If you change your struct to a class and make the members public, it will still compile in C++.
Thanks for the suggestions weaknessforcats, this has two problems also, but also gave me an idea.
One of the problems is that these constants are used everywhere, so they either have to have global visibility or be passed everywhere as a parameter. And the second one is that I also have to super-optimize my program for speed, so passing them as a parameter is a negative, and also the fact that they have to be initialized dinamically and so these prevents a lot of constants calculations that is made with them to be done in the compilation.
I can try to declare them as global variables (not constants), and see how much is the performance hit I get.
For now I'm using (and perhaps abusing) of the ## operator, and have tons of constants like this:
const MyStructure STRUCT0={FIRST, 0} ;
const MyStructure STRUCT1={STRUCT0.Start+5, 25} ;
const MyStructure STRUCT2={STRUCT1.Start+35, 175} ;
#define STRUCT0_START FIRST
#define STRUCT0_ATTRIBUTE 0
#define STRUCT1_START (STRUCT0_START+5)
#define STRUCT1_ATTRIBUTE 25
#define STRUCT2_START (STRUCT1_START+35)
#define STRUCT2_ATTRIBUTE 175
And then I access them by:
#define MyStructGetStart(number) STRUCT##number##_START
#define MyStructGetAttribute(number) STRUCT##number##_ATTRIBUTE
I'll see if I can come with something better, any additional idea is appreciated.
TIA & Regards ...