I have a base class with a static member and a non static function that returns the value of said static member. I also have a derived class that inherits the base class, but assigns an alternate value to the inherited static member.
class Parent
{
protected:
static const int foo = 0;
public:
int GetFoo()
{
return foo;
}
};
class Child : Parent
{
protected:
static const int foo = 1;
};
If I call the inherited getter function on an instance of the derived class, I would expect it to return the value of the static member of the derived class (1). However, it actually returns the value of the static member on the base class instead (0).
int main(int argc, char* argv[])
{
Child child();
std::cout << child.GetFoo();
}
The output is 0
Why was my initial understanding of this interaction incorrect, and how would I attain my expected behavior wherein calling the getter on an instance of the derived class would return the static member of the derived class?
I have a base class with a static member and a non static function that returns the value of said static member. I also have a derived class that inherits the base class, but assigns an alternate value to the inherited static member.
class Parent
{
protected:
static const int foo = 0;
public:
int GetFoo()
{
return foo;
}
};
class Child : Parent
{
protected:
static const int foo = 1;
};
If I call the inherited getter function on an instance of the derived class, I would expect it to return the value of the static member of the derived class (1). However, it actually returns the value of the static member on the base class instead (0).
int main(int argc, char* argv[])
{
Child child();
std::cout << child.GetFoo();
}
The output is 0
Why was my initial understanding of this interaction incorrect, and how would I attain my expected behavior wherein calling the getter on an instance of the derived class would return the static member of the derived class?
Share Improve this question asked Mar 13 at 6:44 Red NeedleRed Needle 411 silver badge2 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 4Because member variables are not virtual. You have shadowed Parent::foo
within the context of Child
but GetFoo()
is a member of Parent
. If you really need this behaviour you need to pay for the cost of indirection somehow. For example:
class Parent
{
protected:
virtual int foo() {
return 0;
}
public:
int GetFoo()
{
return foo();
}
};
class Child : Parent
{
protected:
virtual int foo() override {
return 1;
}
};
GetFoo
is the function of the parent class, so it cannot accessChild::foo
, it accessesParent::foo
. By the way, did you learn aboutvirtual
and late binding? You have to. Before you do it, further answers will hardly make any sense. – Sergey A Kryukov Commented Mar 13 at 6:45static const int foo = 0;
andstatic const int foo = 1
are two different variables in two distinct name scopes. – 3CxEZiVlQ Commented Mar 13 at 6:54int GetFoo()
virtual in the baseclass and add override it in the derived classint GetFoo() override
so that the derived implementation can return the derived static constant (I wouldn't give it the same name). so as @SergeyAKryukov says when learning about inheritance you really should be learning about virtual functions as well. – Pepijn Kramer Commented Mar 13 at 6:54