I have an older source code, like this, in a header used from many places in my project:
const int myVar = myFunc();
What I want:
- Being a global, const variable, I would like if it would be linked only once in the binary.
- Thus, also
myFunc()
should be called only once in the global variable initialization phase.
Now the problem is that I get this warning from the .cc
I compile:
In file included from mySource.cc:7:0:
myHeader.h:59:11: warning: ‘myVar’ defined but not used [-Wunused-variable]
const int myVar = myFunc();
^
Note, mySource.cc
really doesn't use myVar
, thus the warning is okay, but other sources yes.
I think, the best would be if I would declare myVar
only in the header, some like so:
myHeader.h:
int myVar;
mySource.cc:
int myVar = myFunc();
But in this case, I can't declare it as const. This variable should be a const. Yes, I know it will be on a writable memory page, only the c++ will see it as a constant, but this is exactly what I want.
Thus, I also want to avoid this warning. Furthermore, I think myFunc()
would be called many times, what I don't want.
How can I do this?
Aucun commentaire:
Enregistrer un commentaire