Inheritence and "this"

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace 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.
Last edited on
The variable online 7 and the variable on line 15 are not related at all. From the perspective of speak(), the Dog class doesn't even exist.

Look into virtual functions, which will do what you want.
Last edited on
about "this"

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.


Last edited on
@Bdanielz: actually the correct way to write that constructor is with an empty body:
1
2
3
4
someClass(int data)
: data(data) //the compiler isn't stupid
{
}
Generally, you should not initialize members in your constructor body.
Last edited on
Thanks. I was trying use the keyword this and I was setting its value via the constructor. Good call.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>

class Animal {

    public:

        std::string voice = ".";

        Animal() = default ; // default voice

        void speak() const {
            std::cout << voice << ' ' << this->voice << ' '
                      << Animal::voice << ' ' << this->Animal::voice << '\n' ;
        }

    // the constructor of a derived class can specify what its voice is
    protected: Animal( const std::string& its_voice ) : voice(its_voice) {}
};

class Dog : public Animal {

    public: Dog() : Animal( "Whoof!" ) {} // this animal's voice is  "Whoof!"
};

int main() {

    const Animal a{} ; const Animal& a1 = a ; const Animal* pa1 = std::addressof(a) ;
    const Dog d ; const Animal& a2 = d ; const Animal* pa2 = std::addressof(d) ;

    a.speak() ; a1.speak() ; pa1->speak(); // Prints . . . . three times
    d.speak() ; a2.speak(); pa2->speak() ; // Prints "Whoof!" "Whoof!" "Whoof!" "Whoof" thrice
}

http://coliru.stacked-crooked.com/a/4283535ba0802a0c
Topic archived. No new replies allowed.