can i use class instance variables in functions that aren't part of the class?
this is a simple example of my problem.
class person{
public:
person();
~person();
int height;
int weight;
};
int is_he_short(){if(tim.height<70){cout<<"he is short" }
int main(){
peron tim;
tim.height=60;
tim.weight=150;
is_he_short();}
my compiler says tim was not declared in this scope. i know that i caould make the is_he_short function a part of the person class and get rid of tim. in front of height but for what i'm doing i don't want the function part of that class. Make sense? i hope so. please respond
Avoid globals. Writing a function that only works with 'tim' is bad form. It's best to take the person as a parameter, or to have it as a member function. That way your function will work with ANY person, and not just tim.
Globals lead to disorganized, hard to maintain code that is not reusable. Refrain from using them unless it's really the best option (which isn't often the case).
robman: Instead of asking "Will it work?", go ahead and actually TRY IT. :-) Put some back into the learning.
I agree with Disch that globals tend to limit and disorganize. I, however, still think that since you asked about it, you should know about it in case you need it in the future. After all, this is a forum to learn. So know about this construct (global variables), but really analyze the consequences of using them.