C++ Override base member value

Viewed 4377

i have following code:

class A
{
public:
    int foo = 0;
};

class B: public A
{
public:
    int foo = 1;
};

int main()
{
    A *a = new B();

    std::cout << a->foo;
    std::getchar();

    return 0;
}

output: 0

why does B not override the member foo from A ?

and how to get the desired output of 1 without casting to derived class

4 Answers
Related