Home  >  Article  >  Backend Development  >  Why Can\'t I Access Protected Members of My Base Class in a Template Derived Class?

Why Can\'t I Access Protected Members of My Base Class in a Template Derived Class?

Barbara Streisand
Barbara StreisandOriginal
2024-11-01 11:38:02146browse

Why Can't I Access Protected Members of My Base Class in a Template Derived Class?

Understanding Template Inheritance and Non-Dependent Name Lookup

Consider the following code snippet:

<code class="cpp">// arrayListType.h
template <class elemType>
class arrayListType {
protected:
    elemType *list;
    int length;
};

// unorderedArrayListType.h
template <class elemType>
class unorderedArrayListType: public arrayListType<elemType> {
};

// main1.cpp
unorderedArrayListType<int> intList(25);</code>

Compiling this code results in an error: "length was not declared in this scope," indicating that the protected variables of the parent class, arrayListType, are not visible in the inherited class, unorderedArrayListType.

This is due to a concept known as non-dependent name lookup, which defines how the compiler resolves references to non-dependent names within templates. Non-dependent names are those that do not depend on the template parameters, such as member variables or member function names.

In this case, the protected variables within arrayListType are non-dependent names, and they are not explicitly defined in the unorderedArrayListType class definition. The compiler cannot determine that these names exist within the inherited class and therefore reports an error.

Fixing the Error

There are two common approaches to address this error:

Using this-> Prefix:

Prefixing non-dependent names with this-> explicitly indicates that they refer to inherited members:

<code class="cpp">class unorderedArrayListType: public arrayListType<elemType> {
public:
    void insertAt(int location, const elemType& insertItem) {
        this->length++;
        this->list[location] = insertItem;
    }
};</code>

Using Declarations:

Using declarations explicitly declares the non-dependent names within the inherited class:

<code class="cpp">class unorderedArrayListType: public arrayListType<elemType> {
public:
    using arrayListType<elemType>::list;
    using arrayListType<elemType>::length;

    void insertAt(int location, const elemType& insertItem) {
        length++;
        list[location] = insertItem;
    }
};</code>

Both methods effectively communicate to the compiler that the non-dependent names belong to the inherited class and allows the compilation to proceed successfully.

The above is the detailed content of Why Can\'t I Access Protected Members of My Base Class in a Template Derived Class?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn