The problem of nested classes in a template class

I tried to use nested classes inside a template class. See code snippet below:

template <class T>
class OutterTemplate {
public:
    class InnerBase {
    protected:
        const char* name_; 
    public:
        virtual void print() {
            cout << name_ << endl;
        } 

        void setName(const char* n) {
            name_ = n;
        }
    };

private:
    class Inner : public InnerBase {
        public:
            virtual void print() {
                cout << name_;
                cout << " and ";
                InnerBase::print();
            }
    };
public:    
    static InnerBase* getInner() {
        return new Inner();
    }
};

int main() {
    auto q = OutterTemplate<int>::getInner();
    q->setName("Not working");
    q->print();
}

I got the error "error: 'name_' was not declared in this scope" when trying to compile this code. I have a check if "outter" is not a template class, there is no such problem. Can someone explain why this error is related to template classes and how to allow access to class members in case of nested classes inside the template class?

+3
source share
1 answer

The standard in this matter is perfectly clear:

14.6.2 Dependent Names [temp.dep]

3 , -, .

OutterTemplate<T>::InnerBase OutterTemplate<T>::Inner, cout << name_; . , InnerBase . this-> :

14.6.2.1 [temp.dep.type]

7 , , .

this-> - , , name_ , OutterTemplate<T>::Inner , name_ .

+1

All Articles