this-> question
-
Hi All, I was just wondering about something that has been bugging me ever since I saw something like this in Java. In practice, which type of statement would be more preferable if you were to call a class function inside the same class in C++?
class Object {
void func1() { this->func2() }
void func2() {}
};or
class Object {
void func1() { func2() }
void func2() {}
};Is there any performance differences between the two? :confused: Also, I apologize if this code might look wrong. I just wanted a conceptual opinion.:) Frank
-
Hi All, I was just wondering about something that has been bugging me ever since I saw something like this in Java. In practice, which type of statement would be more preferable if you were to call a class function inside the same class in C++?
class Object {
void func1() { this->func2() }
void func2() {}
};or
class Object {
void func1() { func2() }
void func2() {}
};Is there any performance differences between the two? :confused: Also, I apologize if this code might look wrong. I just wanted a conceptual opinion.:) Frank
No direrences.... Best Regards... Carlos Antollini.
-
Hi All, I was just wondering about something that has been bugging me ever since I saw something like this in Java. In practice, which type of statement would be more preferable if you were to call a class function inside the same class in C++?
class Object {
void func1() { this->func2() }
void func2() {}
};or
class Object {
void func1() { func2() }
void func2() {}
};Is there any performance differences between the two? :confused: Also, I apologize if this code might look wrong. I just wanted a conceptual opinion.:) Frank
There's no performance difference. IMHO, you only need 'this->' when one of the method parameters has name colliding with name of another data member. In the example below, the parameter 'foo' hides the member 'foo', so you need to explicitly qualify it with 'this'. Of course, you can rename the SetFoo parameter 'foo' to 'theFoo', in such case there's no need for explicit 'this'.
class obj
{
public:
void SetFoo(int foo) { this->foo = foo; }
int GetFoo() const { return foo; }
private:
int foo;
};Tomasz Sowinski -- http://www.shooltz.com