Let's say I have following code:
// I'll be providing these 3
int a = 0;
void setA(){ a = 1000;}
void processA() { std::cout << a; }
// User will call them
int main(){
// setA(); // <-- Notify about this
processA();
return 0;
}
Here I must make sure that someone has called setA() before calling processA(). I've thought about 2 ways to do this:
- call
setA()just insideprocessA()function or just dump the contents ofsetA()insideprocessA() - maintain a boolean flag and check at later state to make sure
setA()has been called
Now 1st method makes sense but the problem is setA() function would be called a lot earlier than processA() and I just cannot do this. 2nd way is fine but is runtime dependent.
Is there a way to make this work in compile time, so that there is a message/warning or a compiler warning would be the best way to alert user that he/she hasn't called setA() before processing it.
Aucun commentaire:
Enregistrer un commentaire