Class member function / member variables

Hi :)

My question is what kind of method should i use for changing my membervariables in memberfunctions. So far theres 3 different methods that are same, so what should i choose?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class number{
public:
	number():a(10),b(10),c(10){}
	void increment(int x);

	int a;
	int b;
	int c;
};

void number::increment(int x)
{
	a += x;
	this->b += x;
	number::c += x;
}

int main()
{
	number mynumber;
	mynumber.increment(2);
	return 0;
}


So what is the best? fastest? most efficient? most widely used?
In my opinion the first method is the method of choice. Your member function has full access to all members of your class so no access specifiers should be required. In my opinion you should rarely need the "this->" method if you use proper unique variable and parameter names. The scope operator:: should be reserved primarily for use when you need to specify a member variable from a base class.
They are equivalent. There is no need to specify this or number:: in your simple example. So I would prefer to write simply

a += x;
Topic archived. No new replies allowed.