最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

inheritance - Access derived class static members from inherited base class function in c++ - Stack Overflow

programmeradmin2浏览0评论

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
  • 2 GetFoo is the function of the parent class, so it cannot access Child::foo, it accesses Parent::foo. By the way, did you learn about virtual 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:45
  • 2 This question is similar to: Is it possible to declare a virtual static constant value in a C++ class?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – pptaszni Commented Mar 13 at 6:50
  • 1 static const int foo = 0; and static const int foo = 1 are two different variables in two distinct name scopes. – 3CxEZiVlQ Commented Mar 13 at 6:54
  • 2 For this to work you must make int GetFoo() virtual in the baseclass and add override it in the derived class int 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
  • 1 AlexAnder, because it leaks implementation details of the baseclass to derived classes. Creating a tighter coupling between base and derived other than overriding virtual methods. So it is something I do because of experience I have with having to refactor code bases (and the less coupling between classes the easier that is). Or in other words, don't use inheritance for code reuse but only override virtual methods (Liskov substition principle in action). – Pepijn Kramer Commented Mar 13 at 8:23
 |  Show 5 more comments

1 Answer 1

Reset to default 4

Because 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;
    }
};
发布评论

评论列表(0)

  1. 暂无评论