I know we can use global variable in our smart contract. So, How can I make sure that nobody can access my global variable.
2 Answers
Global variables are more or less a design thing. I personally do not like them because of object-orientation for languages like c++. They are good for script languages, but if your application or contract gets complex, things get complicated and they are hard to maintain.
However you do not have to worry about access of our global variable because of the application isolation of the WebAssembly virtual machine. Also they only are accessibly during your action excecution. Your contract code/variables/members will be reinitiated on every action call. If you want to persist your variables you have to use the multi_index (aka Persistence API).
It may be possible to store abitrary members/attributes of your contracts in the table if you select a fitting table schema. Maybe something like this:
ID (unint64_T) | Attribut (String) | Value (String)
0 | 'mCounter' | '5'
1 | 'mComplexAttribut'| '{ key: 1, key2: {...}}'
Global variables should be used sparingly in C++ programs and only used when absolutely necessary. It's good practice to keep variables limited in scope (using keywords static
, private
, etc) to protect unintentional modification of their value. You can create functions to allow modification of these private members from other parts of your program.