When programming a class in C++, I'm wondering if there is any speed difference or other negative reasons for using the 'this' pointer to access data members. Its harder for me to explain, but easier to show with code. I'm wondering if these 2 class methods have any difference from eachother.
Code:
class Test
{
private:
string data_;
public:
void setData1(string& data);
void setData2(string& data);
};
void Test::setData1(string& data)
{
// use 'this' for extra clarity
this->data_ = data;
}
void Test::setData2(string& data)
{
// don't use 'this'
data_ = data;
}
I prefer using the 'this->data_member' notation because it makes my classes easier to read. I'm wondering if there is any negative impact to using this notation.
I was also wondering the same thing about the resolution operator, '::'. I can't see any negative effects of using it in classes like this:
Code:
// same class definition as above
void Test::setData1(string& data)
{
// Use scope resolution operator
Test::data_ = data;
}
void Test::setData2(string& data)
{
// Dont use scope resolution operator
data_ = data;
}