Ob das sinnvoll ist, ist eine andere Frage.
Das Problem hier ist m.E., daß jede Methode, die für
ein volatile-Objekt aufgerufen wird, auch als volatile-geeignet
deklariert werden muß mit einem angehängten volatile (ähnlich
wie angehängtes const für const-Objekte).
Dabei können die Methoden für volatile und nicht-volatile
getrennt überladen werden:
1 | #include <iostream>
|
2 |
|
3 |
|
4 | class Steppermotors
|
5 | {
|
6 | public:
|
7 | Steppermotors()
|
8 | : i(1)
|
9 | {
|
10 | }
|
11 |
|
12 | void tuwas()
|
13 | {
|
14 | std::cout << "ich tu was (normal)..." << std::endl;
|
15 | }
|
16 |
|
17 | void tuwas() volatile
|
18 | {
|
19 | std::cout << "ich tu was (volatile)..." << std::endl;
|
20 | }
|
21 |
|
22 | private:
|
23 |
|
24 | int i;
|
25 | };
|
26 |
|
27 |
|
28 | volatile Steppermotors st_vol;
|
29 | Steppermotors st_nonvol;
|
30 |
|
31 |
|
32 |
|
33 | int main( int nargs, char **args )
|
34 | {
|
35 |
|
36 | st_vol.tuwas();
|
37 | st_nonvol.tuwas();
|
38 |
|
39 | return 0;
|
40 | }
|
Ausgabe:
1 | klaus@i4a:~ > g++ -Wall t.cpp && ./a.out
|
2 | ich tu was (volatile)...
|
3 | ich tu was (normal)...
|
Die nicht-volatile-Version kann auch weggelassen werden, dann
wird die volatile-Variante sowohl für volatile- als auch
nicht-volatile-Objekte verwendet:
1 | #include <iostream>
|
2 |
|
3 |
|
4 | class Steppermotors
|
5 | {
|
6 | public:
|
7 | Steppermotors()
|
8 | : i(1)
|
9 | {
|
10 | }
|
11 |
|
12 | void tuwas() volatile
|
13 | {
|
14 | std::cout << "ich tu was (volatile)..." << std::endl;
|
15 | }
|
16 |
|
17 | private:
|
18 |
|
19 | int i;
|
20 | };
|
21 |
|
22 |
|
23 | volatile Steppermotors st_vol;
|
24 | Steppermotors st_nonvol;
|
25 |
|
26 |
|
27 |
|
28 | int main( int nargs, char **args )
|
29 | {
|
30 |
|
31 | st_vol.tuwas();
|
32 | st_nonvol.tuwas();
|
33 |
|
34 | return 0;
|
35 | }
|
Das erzeugt:
1 | klaus@i4a:~ > g++ -Wall t.cpp && ./a.out
|
2 | ich tu was (volatile)...
|
3 | ich tu was (volatile)...
|