How to create externally unmodifiable variable?

  • A+
Category:Languages

I'm developing simple simulation library and came across problem, where I have simulation Time variable, which shouldn't be modifiable by API user (programmer) at any circumstances (just provide information about simulation time), but should be modifiable by simulation library, so it cannot be constant.

This is what i came up with yet but it seems a little bit tricky to me

double simTime;                // Internal time, modified by library const double& Time = simTime;  // Time info provided for programmer in API 

Is there any better approach for this?

 


Instead of const double & you can change your API to provide a function double getTime(); which returns the value of simTime.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: