Inheritance/macros

Basically... I want to create some variables which, unless specified at compile time, aren't included in the compile.

I was thinking of doing it like this:

3 structs, each contains the variables I want to include/exclude. I then create a class which inherits from all 3 structs.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

struct somestruct1 {
  int var1;
};

struct somestruct2 {
  int var2;
};

struct somestruct3 {
  int var3;
};

struct mymainstruct: public somestruct1, somestruct2, somestruct3 {
};


Essentially the same as: (right?)

1
2
3
4
5
6
7

struct mymainstruct {
  int var1;
  int var2;
  int var3;
};


If it is...

...(next bit)...

...is is appropriate to use macros like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#define RS_INC2

struct somestruct1 {
  int var1;
};

struct somestruct2 {
  int var2;
};

struct somestruct3 {
  int var3;
};

struct mymainstruct: public somestruct1
					#ifdef RS_INC2
					  , somestruct2
					#endif
							#ifdef RS_INC2
							  , somestruct3
							#endif
{
  int total = 0;
  total += var1;

  #ifdef RSINC2
    total += var2;
  #endif

  #ifdef RSINC3
    total += var3;
  #endif

};


I know this is gonna make a lot of people laugh/cringe, but it would achieve my goal, right?

I won't have more than 1 instance of this in my code... and the compiled memory size/program size is FAR more important than tidy code/or blindly followed standards. This is for an MCU....

Is there a better way to do what I want?
closed account (o3hC5Di1)
Why don't you put those variables in an external file and only include the file in the compilation when you want to?

Why you would do this seems weird to me - this is asking for compiler errors about "undefined variables" etc.

Also, if your first example and your second are the same, as you indicate yourself, why would you opt for the longer and more obfuscated one?

I'm not criticising, I'm too much of a beginner myself, just interested at why you are taking this route.

All the best,
NwN

Why don't you put those variables in an external file and only include the file in the compilation when you want to?

Why you would do this seems weird to me - this is asking for compiler errors about "undefined variables" etc.



I figure, including/excluding a header file with bits of code amounts to the same thing... dodgy code :-/ right? I would LIKE the source to be constant whether or not a #define flag is passed in.

The inheritance isn't the point. I could declare 3 structs and put them all IN another struct... in fact, this would probably be a preferable structure... but I would still have to macro out the struct variables in the master struct.... just as dodgy :)


Also, if your first example and your second are the same, as you indicate yourself, why would you opt for the longer and more obfuscated one?



The problem with the second is that the code with contain more bytes than its going to need. This isn't an option.


I'm not criticising, I'm too much of a beginner myself, just interested at why you are taking this route.



:)
Last edited on
Why do you use preprocessor to cut out the inherited structures and not the variable declaration themselves?

1
2
3
4
5
6
7
8
9
struct mymainstruct {
  int var1;
#ifdef RSINC2
  int var2;
#endif
#ifdef RSINC3
  int var3;
#endif
};


Also if the variables are of the same type - then you can do the following:
1
2
3
4
template<int N_VARS>
struct mymainstruct {
   int vars[N_VARS];
};

This will have no redundancy as well and will depend on how you instantiate the template. This will allow you to concentrate all the ifdefs in one place - where this structure is used.
Why do you use preprocessor to cut out the inherited structures and not the variable declaration themselves?


I put them in structs because another struct, say masterstructB will have them all. this struct will be used off the mcu and having those extra few bytes makes no difference to a desktop based system.

Also if the variables are of the same type - then you can do the following:


they are not. the code above was written to ask my question only. there are lots of variables and a few functions spanning those child structs.

ummmmm....
Topic archived. No new replies allowed.