1

This title is a little misleading. Here's my question:

In the following simple example, A has a public data member a of type T. B is a derived class of A, since there's no ambiguity, we can use A::a under the name a in B::fun. But it turns out that g++ (4.7.0) will complain error: ‘a’ was not declared in this scope

If A and B are normal classes, there is no such problem.

My question is: is this the standard behavior of c++ compiler? what is that rationale of this behavior?

template<typename T>
struct A {

    T a;
};

template<typename T>
struct B: public A<T> {

    void fun() {

        // does not compile
        // for normal class (non-template class), this is ok
        a = 10;

        // ok
        A<T>::a = 10;
    }
};

int main(void) {

    return 0;
}
PeopleMoutainPeopleSea
  • 1,492
  • 1
  • 15
  • 24

0 Answers0