Why doesn't clang allow a derived class to call a protected base function? [duplicate]

By : xmllmx
Source: Stackoverflow.com
Question!
template<int N>
struct B
{
protected:
    void f() {} 
};

template<int N>
struct A : B<N> 
{
    A()
    {
        this->f(); // ok
        f(); // error : use of undeclared identifier 'f'
    }
};

int main()
{
    A<8> a;
}

My C++ compiler is clang 3.8.

Why doesn't clang allow a derived class to call a protected base function?

By : xmllmx


Answers

This is nothing specific to clang but C++ access control and protected access specifier.

The language specification wants to ensure that you are accessing a protected member of some base subobject that belongs to the derived class object. You are not supposed to be able access protected members of some unrelated independent objects of base type.

For this reason, you have to access protected members through pointer->member syntax, reference.member or object.member syntax, where the pointer/reference/object refers to the derived class.

This is exactly inline with what you have shown in your example:

template<int N>
struct B
{
protected:
    void f() {} 
};

template<int N>
struct A : B<N> 
{
    A()
    {
        this->f(); // ok
        f(); // error : use of undeclared identifier 'f'
    }
};

Link: Protected members are not as private as private members, which are accessible only to members of the class in which they are declared, but they are not as public as public members, which are accessible in any function.

Protected members that are not declared as static are accessible to friends and member functions in a derived class only through a pointer to, reference to, or object of the derived class.



What if you try to put this Annotation (see below) for the property that is not being populated:

[SitecoreField(FieldName = "MenuHeaderItem", FieldType = SitecoreFieldType.Droplist)]


I'd do it with zip and list comprehension.

>>> a = [2, 4, 1, 1, 6, 1, 1, 3, 5, 1]
>>> b = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
>>> c = [x[0] for x in zip(b, a) if x[1] == 1]
>>> c
['C', 'D', 'F', 'G', 'J']
>>>


This video can help you solving your question :)
By: admin