How can a derived class use a protected member of the base class?

前端 未结 2 974
礼貌的吻别
礼貌的吻别 2020-12-21 03:49

Say that a base class A defines a protected member. A derived class B uses this member.

class A
{
public:
  A(int v) : value(v) { }         


        
相关标签:
2条回答
  • 2020-12-21 04:30

    There is actually a loophole using member pointers (no casting, no copying):

    void B::compare_and_print(const A& other) const
    {
      auto max_value = std::max(value, other.*(&B::value));
      std::cout << "Max value: " << max_value << "\n";
    }
    
    0 讨论(0)
  • 2020-12-21 04:44

    You can bypass protected by using a helper struct:

    struct A_Helper : public A
    {
        static int GetProtectedValue(const A & a)
        {
            return static_cast<const A_Helper&>(a).Value;
        }
    };
    

    You can get it by using it anywhere A_Helper::GetProtectedValue(a)

    In your case, you could cast other to const B& (via static_cast or reinterpret_cast) but you don't know if instance of other is of type B. With that casted value people reading the code would presume that other is of type B and could insert code that causes reads/writes to "random" memory.

    Consider that class B has another memeber value_B and other is of type C. Using static_cast<const B&>(other).value_B is Undefined Behavior.

    0 讨论(0)
提交回复
热议问题