mardi 23 juillet 2019

Modifying an Inherited Member Variable Does not Affect Base Class

I have the following two classes:

class A
{
public:

    A() : value(false) {}

    bool get() const
    {
        return value;
    }

protected:
    bool value;
};

class B : public A
{
public:

    void set()
    {
        value = true;
    }
};

Now I use them as follows:

B* b = new B;
std::shared_ptr<A> a(b);

auto func = std::bind(&B::set, *b);

std::cout << a->get() << std::endl;
func();
std::cout << a->get() << std::endl;

I expect a->get() to return true on the 2nd call, but func() hasn't modified its value. Why is that?

Aucun commentaire:

Enregistrer un commentaire