As it stands right now, I'm not finding OOP very useful because "this" is not referencing what you would expect. It doesn't seem like the object is able to do much work when it can't reference the highest level class. For example:
#include <iostream>
#include <string>
usingnamespace std;
class Animal {
public:
string voice = "";
void speak() {
cout << this->voice << endl;
}
};
class Dog : public Animal {
public:
string voice = "Whoof!";
};
int main() {
auto a = new Animal();
auto d = new Dog();
a->speak(); // Prints "" correctly
d->speak(); // Prints "" instead of "Whoof!"
return 0;
}
So my issue here, I feel, is a valid one. My major concern is not actually with property inheritance as much as it is with "this" not seeming to reference the highest level class in it's MRO chain. I've been so frustrated lately trying to find some way to deal with this. Any help would be GREATLY appreciated.
Where I find this useful is when is situation like below.
1 2 3 4 5 6 7 8
class someClass{
private:
int data;
public:
someClass(int data){
this->data = data;
}
};
Without using "this" I would have to change either my classes member name or the param to the constructor, then leading to harder to read code; Using this is optional, but I try to use as much as possible to also ensure I am retuning/using the data of my object I referencing to.