You cannot automatically apply a function to all objects of MyClass (unless you do work for it, but that is more advanced).
The system does not keep track of where the objects are, you have to point out what you want to call yourself.
The simplest form of calling a function on them is to list all calls explicitly:
var1.doSomething();
var2.doSomething();
var3.doSomething();
If you don't make separate variables, but add the objects to a container like an array, you can loop over them:
MyClass objects[5]; // Make 5 'MyClass' objects.
for (int i = 0; i < 5; i++) {
objects[i].doSomething(); // Call the function on the i'th object.
}
(object is the official name for the data attached to variable like your "var1").
Arrays are very static, above you have 5 objects, always. There are other forms (std::list, std::vector), but they come with new challenges to consider.
As for 'this', consider the following class + code:
class AB {
public:
int value;
AB() { value = 4; };
void g1(int val) { // Set the 'value', simple version.
value = val;
}
void g2(int val) { // Set the 'value', what is really happening.
this->value = val;
}
void g3(int value) { // Set the 'value', confusing version.
value = value; // FAIL: it assigns the parameter and not the class member.
this->value = value; // Works.
}
};
AB v; // Create an object named 'v' of the class.
AB w; // Create an object named 'w' of the class.
v.g1(5); // Call 'g1' of 'v'
w.g1(6); // Call 'g1' of 'w'
Function g1 set the value of a variable of the object that you call. With "v.g1(5)", variable "v.value" is changed, while with "w.g1(6)", variable "w.value" is changed. In particular, the second call should not change the value of "v.value".
Note that inside "g1", there is no "v", or "w" (they are defined later). You may even define them in another file, or use a different name. In the array example, there is only "objects" and that contains 5 objects rather than one!
So how does "value = val" in g1 know what to change?
Simple, besides the "v" and "w" name that you create, there is also a "this" name created by the compiler. "this" only exists inside a method in a class, and points to "myself". So for the g1 function of "v", it points to "v" inside the g1 function. For the g1 function of "w", it points to "w" inside the g1 function.
Function g2 shows what g1 really does. g2 does the same as g1, but explicitly refers to itself by "this->", saying "I am changing my own value here".
In this example, it is not needed g1 works just as fine. However there are cases where it is useful to be able to point to yourself, for example when an object passes itself as a parameter to a function. A contrived example is g3, where I 'accidently' added a "value" parameter to the function. Now if you do "value = value" here, it fails. It would be equal to "val = val" in the g1 function. (in the g1 case it's just a lot less confusing what happens ).
If you use "this->value = value" instead, you say "assign value (the parameter) to "my own" value (the value in the class).
Obviously, it's much less confusing if you use a different parameter name, but it's nice to know you can still access and change everything even if you pick unlucky names for variables and parameters.