How would I be able to put these together?

Instead of calling in two different pointers to new; how would I make an instance where I just need to call one in. Instead of creating sepreate u and p just call one pointer to get both of the virtual methods ,or is something like that takes up to much memory. Here's the code.
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
#include <iostream>

using namespace std;
class Thisthing
{
public:
    virtual void Eatsomething()
    {
        cout << "He is being eaten" << endl;
    }
private:
    int Dont_Eat_Him;
};
class Bringit : Thisthing
{
public:
     void Eatsomething()
     {
         cout << "He's going to be ate" << endl;
     }
};

int main()
{
    Thisthing *p = new Thisthing;
    Bringit *u = new Bringit;
    p->Eatsomething();
    u->Eatsomething();
}
Also I am not new to polymorphism or dynamic memory by a long shot. I just wanted to know if I can do something where you only need one pointer to Thisthing and Bringit. This is the way it was taught to me ,but by my small expereince there is always a different way to do something.
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
34
35
36
#include <iostream>

class Thisthing
{
    public:
        virtual ~Thisthing() {} // = default ; // http://www.stroustrup.com/C++11FAQ.html#default

        virtual void Eatsomething() const // const
        {
            std::cout << "He is being eaten\n" ; // << endl;
        }
    private:
        // int Dont_Eat_Him = 0 ; // http://www.stroustrup.com/C++11FAQ.html#member-init
};

class Bringit : public Thisthing // note: public
{
    public:
        virtual void Eatsomething() const // override // http://www.stroustrup.com/C++11FAQ.html#override
        {
            std::cout << "He's going to be ate\n" ; // << endl;
        }
};

int main()
{
    Thisthing* pointer = 0 ; // = nullptr; // http://www.stroustrup.com/C++11FAQ.html#nullptr

    Bringit bring_it ;
    pointer = &bring_it ; // = std::addressof(bring_it); // http://en.cppreference.com/w/cpp/memory/addressof
    pointer->Eatsomething();

    Thisthing this_thing ;
    pointer = &this_thing ; // = std::addressof(this_thing);
    pointer->Eatsomething();
}

http://coliru.stacked-crooked.com/a/4d912b9fd384fdeb
So there is pretty much no way of doing it like I said it I knew it. But yeah I actually have defined it by refrence before hand. Thanks anyway @JLBorges
You asked for a way to do it with one pointer and JLBorges showed an example of doing that. Are you asking if there is a way to access both functions through a single object? You can't do that. The reason is that with virtual functions, the function that gets called is basically a property of the object itself.

Are you asking if there is a way to access both functions through a single object? You can't do that.
You can if you want.
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
#include <iostream>

class Thisthing
{
public:
    virtual void Eatsomething()
    {
        std::cout << "He is being eaten\n";
    }
private:
    int Dont_Eat_Him;
};
class Bringit : public Thisthing
{
public:
     void Eatsomething()
     {
         std::cout << "He's going to be ate\n";
     }
};

int main()
{
    Thisthing *p = new Thisthing;
    Bringit *u = new Bringit;
    p->Eatsomething();
    std::cout << '\n';
    u->Eatsomething();
    u->Thisthing::Eatsomething();
}
He is being eaten

He's going to be ate
He is being eaten

Thanks @MiiiNiPaa that is exactly what I was looking for.
Topic archived. No new replies allowed.