I read that protected members are private in derived classes, so it seems like this is normal, but it is not.
This is not because the protected item inherited from the base class A ( root , in this case) by the derived class B ( aa , in this case) is available for how long since it accesses an object of type B ( aa ). Here you access it through an object of type A ( root ):
root* aa::add(const root& a) { for (int i=0; i<a.size; i++)
In paragraph 11.4 / 1 of the C ++ 11 standard:
The extra access control beyond the limits described in section 11 above applies when the non-static data member or non-static member function is a protected member of its naming class (11.2). As previously described, access to a protected member is granted because the link is found in a friend or member of a class C. If access consists in forming a pointer to a member (5.3.1), the naming specifier should denote C or a class derived from C. All other calls include an (possibly implicit) expression of the object (5.2.5). In this case, the class of the expression of the object must be C or a class derived from C. [Example:
class B { protected: int i; static int j; }; class D1 : public B { }; class D2 : public B {
- end of example]
To fix this, you need to provide public setters / recipients. You already have a getSize() function, so instead of writing:
for (int i=0; i<a.size; i++)
You can write this:
for (int i=0; i<a.getSize(); i++)
Similarly, you will need to provide functions to get / set the value of the nth element of the array so you can write:
array[i] += a.get_at(i);
Note that the expression on the left is += OK, because array accessed through this (see also the example above from the C ++ 11 standard).
source share