0

I've got a class which, for reasons unrelated to this question, needs to be a template. This class currently contains a sub class. I'm now trying to make another sub class which is very similar in functionality to the first one, so I thought I'd make it a child. However, I can't get this to work: the child class can't seem to access any protected elements of the parent.

Here's a MWE:

#pragma once

template <typename T>
class testInheritance
{
public:

    testInheritance() : _someTypedStuff(T())
    {}

    // Subclasses :

    class subclassA; // This subclass will be the parent
    class subclassB; // This subclass should inherit from subclassA

    subclassA getClassA() { return subclassA(); }
    subclassB getClassB() { return subclassB(); }

protected:

    T _someTypedStuff; // Some junk to justify having a template

};

template <typename T>
class testInheritance<T>::subclassA {

public:

    subclassA() : _someNumber(10) {} // Set `_someNumber` to 10

    int test() {
        return _someNumber; // Returns 10
    }

protected:
    int _someNumber; // This is set to "10" in the constuctor

};

template <typename T>
class testInheritance<T>::subclassB : public testInheritance<T>::subclassA {

    // Call subclassA's constructor, setting `_someNumber` to 10
    subclassB() : subclassA() {}

    int anotherTest() {

        // Here is the compiler error:
        return _someNumber + 1; // "error : '_someNumber' was not declared in this scope"
    }
};

Is there a magic incantation to get this right, or am I doing something conceptually silly?

CharlieB
  • 704
  • 7
  • 14

0 Answers0