-2

I have 3 classes, 1 inherited by another. A->B->C. A has a protected member function that I'm trying to set using C.

I'm getting C2248 - Error C2248 'A::status': cannot access inaccessible member declared in class 'A' Associations

Am I not allowed to access to the variable in class C?

 class A {
 public:
     A();
     ~A();
 protected:
     char status[4];
 };

 class B: class A {
 public:
     B();
     ~B();
 };

 class C: class B {
 public:
     C(char newStatus[4]);
 };
 C::C(char newStatus[4])
 {
     this.status = newStatus;
 }
S. Gu
  • 1

1 Answers1

0

The default inheritance strategy is private. That means that status will turn into a private member in B and will be inaccessible in C, see Difference between private, public, and protected inheritance for more info. Hence you want to inherit publicly. Also arrays do not support assignment, use std::array instead.

#include <array>

class A {
public:
    A();
    ~A();

protected:
    std::array<char,4> status;
};

class B : public A {
public:
    B();
    ~B();
};

class C : public B {
public:
    C(std::array<char,4> const &newStatus);
};
C::C(std::array<char,4> const &newStatus) {
    this->status = newStatus;
}
Henri Menke
  • 10,705
  • 1
  • 24
  • 42