Ich habe eine große Struktur mit Konfigurationsdaten.
Ich möchte auf die Struktur so zugreifen, dass ich sie nicht aus
versehen verändern kann, möchte aber nicht nicht die komplette Struktur
zurückgeben da dies viel zu viel zeit kostet wenn man nur einen wert
daraus möchte.
Nun sehe ich zwei Lösungen, ein funktioniert sicher ist aber relativ
aufwendig, bei der anderen bin ich mir nicht sicher ob sie das macht was
ich möchte. Sprich ob ich nicht "aus Versehen" die Konfiguration
verändern kann.
Sicher? (leider muss man zwei Sachen updaten und auf Konsistenz prüfen)
1 | typdef struct{
|
2 | float bla0;
|
3 | float bla1;
|
4 | float bla2;
|
5 | }conf_struct_t;
|
6 |
|
7 | typdef enum{
|
8 | bla0 = 0, bla1 = 1,bla2 = 2
|
9 | }conf_enum_t;
|
10 |
|
11 | union{
|
12 | conf_struct_t conf;
|
13 | float *value;
|
14 | }conf_union;
|
15 |
|
16 |
|
17 | float get_conf(conf_enum_t struct_member){
|
18 | return conf_union.value[struct_member];
|
19 | }
|
20 |
|
21 | //functionsaufruf:
|
22 |
|
23 |
|
24 | iNeedConf = get_conf(bla1);
|
elegant?
1 | typfdef struct{
|
2 | float bla0;
|
3 | float bla1;
|
4 | float bla2;
|
5 | }conf_t;
|
6 |
|
7 | conf_t conf;
|
8 |
|
9 | const *conf_t get_conf(void){
|
10 | return &conf;
|
11 | }
|
12 |
|
13 | //functionsaufruf:
|
14 |
|
15 |
|
16 | iNeedConf = get_conf()->bla2;
|
sicher aber sehr langsam?
1 | typfdef struct{
|
2 | float bla0;
|
3 | float bla1;
|
4 | float bla2;
|
5 | }conf_t;
|
6 |
|
7 | conf_t conf;
|
8 |
|
9 | conf_t get_conf(void){
|
10 | return conf;
|
11 | }
|
12 |
|
13 | //functionsaufruf:
|
14 |
|
15 |
|
16 | iNeedConf = get_conf().bla2;
|