Passing local variable to a global function
In my program i am passing a locally constructed variab开发者_开发百科le to a global class object's member function. The member function will assign this variable to a private member and use it through out the program. Is there any drawback in this approach?
public void function()
{
int a = 0;
globalClassObject.StoreValue(a);
}
This is fine. The problem would be if you would pass a reference to this variable. In this case the value is "copied" to the variable within the function so the original a
variable isn't actually used.
I don't see a problem with this. The variable is used in local scope only, so no unexpected results here.
The big drawback is if this value can be changed anywhere else in the program. If it can, your program will become a swamp for bugs to breed. If the globalClassObject stores the value immutably, there is no problem.
精彩评论