I dont really understand the 'this' pointer. You need it to initialize its data members, but yet its optional for using it in the member functions, and apparently calling the member functions as well. Is there any way to remove the need to use the this pointer in the constructor? IT seems like it should be there all or nothing sort of. is "this" pointer essentially the equivalent of Python's "self"?
You need it to initialize its data members, but yet its optional for using it in the member functions
Technically not. The reason you need it in your constructor there is because you have parameter names that match member names.
1 2 3 4 5 6 7 8 9 10
class Foo
{
private:
int bar;
public:
Foo( int bar )
{
this->bar = bar; // 'this' referring to member, not parameter
}
};
You could avoid it by having different parameter names. Or, preferably, by using initialisation lists for you constructor. They can distinguish between variables with the same name without the explicit this pointer.
1 2 3 4 5 6 7
class Foo
{
private:
int bar;
public:
Foo( int bar ): bar( bar ) {}
};