member function changing class variables

hello everyone, I'm trying to learn about classes. I've got the basics down. but one thing that I'm confused about that i cant find the answer to is if a class has a member function and some variables. what the function does is change the variables. what do i put in the member function? would it be like this:

1
2
3
4
void Class::function()
{
Class::variable1 = Class::variable2;
}


or

1
2
3
4
void Class::function()
{
Class.variable1 = Class.variable2;
}


or the more likely option neither of these. also function has to be for any instance. so when i call that function the variables for the same instance change. i hope I'm not being to confusing, thanks for the help.
Neither is correct. The first would only be correct if they were static class variables. Because you're in that Class::function(), the variables is already in scope, and you don't need to do anything at all to get to them. However if one of the parameters has the same name as a variable you can use this->variable to specify you want to use the variable stored in the class and not the parameter.

The function already knows what instance it is being called for, you don't need to worry about that. Only static functions are not called from an instance of the class.
Last edited on
Topic archived. No new replies allowed.