C++ - How to access private members of a class, from a static function of the same class?

后端 未结 4 846
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 05:11

What I have:

So I have a class with a private member, and a static function. The function must really be static and I can\'t change that.

4条回答
  •  心在旅途
    2021-01-17 05:40

    A static member function cannot access a non-static member (unless it creates its own local instance the non-static member would belong to).

    This is because non-static members belong to an instance of the class, and the static member does not. Think about it: If you wrote

    Base::callback(...);
    

    what m_member should this access? There simply is no instance of Base and thus not m_member.

提交回复
热议问题