academic question - usage of class variable
-
This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav
-
This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav
-
This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav
Generally speaking, a class variable (a static variable of a class) should not (like an ordinary member variable) directly accessed by the external world, that would go against information hiding. Moreover, you don't need need (and you should not) to pass such variables either to class (static) methods or standard (instance) methods.
-
Generally speaking, a class variable (a static variable of a class) should not (like an ordinary member variable) directly accessed by the external world, that would go against information hiding. Moreover, you don't need need (and you should not) to pass such variables either to class (static) methods or standard (instance) methods.
I probably did not make myself clear in my post. I have a class function / method which manipulates class variable - so how do I make such variable "variable" ? Simple example - if I have need to execute "switch" by varying it. I can either change the class variable or pass the value to a function.
-
I probably did not make myself clear in my post. I have a class function / method which manipulates class variable - so how do I make such variable "variable" ? Simple example - if I have need to execute "switch" by varying it. I can either change the class variable or pass the value to a function.
-
This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav
Vaclav_ wrote:
Would assigning it outside the function defeat the "scope" of the class?
The term "scope" is only meaningful to places where your class object is used (i.e., the owners of class instances). What a class does internally, on its own member variables, is not important to "outsiders."
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
This may be silly to some , but I have to ask. If the class function / method "works on" class variable should such variable value be passed to the function(class variable) or just defined / assigned outside function(void) / method? Would assigning it outside the function defeat the "scope" of the class? Cheers Vaclav
Generally, if you define a member variable of a class, this variable should only be accessed by member functions of that class (or derived class). This is true both for static and non-static member variables. If you need to read or change them from outside the class, define accessor member functions as needed. Any static or non-static function can and may access any member variable. Therefore there is no need to pass it to such a function, unless it is a non-static member variable of a different instance of the class. Example:
class Vector3i {
private:
std::array data;
public:
// example 1: change own member variable
void fill(const int value) {
data.fill(value);
}
// example 2: change member variable from another instance
void swap(Vector3i& other) {
std::swap(data, other.data);
}
};GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)