In my program i am passing a locally constructed variable 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);
}
3 个解决方案
#1
2
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.
这可以。问题是如果您要传递对此变量的引用。在这种情况下,该值被“复制”到函数内的变量,因此实际上并未使用原始变量。
#2
0
I don't see a problem with this. The variable is used in local scope only, so no unexpected results here.
我没有看到这个问题。该变量仅在本地范围内使用,因此此处没有意外结果。
#3
-1
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.
最大的缺点是如果可以在程序中的任何其他位置更改此值。如果可以的话,你的程序将成为繁殖bug的沼泽地。如果globalClassObject以不可变的方式存储该值,则没有问题。
#1
2
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.
这可以。问题是如果您要传递对此变量的引用。在这种情况下,该值被“复制”到函数内的变量,因此实际上并未使用原始变量。
#2
0
I don't see a problem with this. The variable is used in local scope only, so no unexpected results here.
我没有看到这个问题。该变量仅在本地范围内使用,因此此处没有意外结果。
#3
-1
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.
最大的缺点是如果可以在程序中的任何其他位置更改此值。如果可以的话,你的程序将成为繁殖bug的沼泽地。如果globalClassObject以不可变的方式存储该值,则没有问题。