Template class heritance strange behaviour

rodrigo benenson rodrigo.benenson@inria.fr
Wed Mar 14 14:22:00 GMT 2007


I have a question about the namespace management when heriting a
templated class.

Given the following code, why the member variable 'c' is managed
differently than the member method 'set_c' ?

Thanks for you explanations.

rodrigob.
http://www-rocq.inria.fr/~benenson/


The following code compiles.
//////////////////////////////////////////
template<typename T1>
class Parent
{

public:

  T1 c;

  void set_c(T1 &val)
  {
    c = val;
  }

};


template<typename T2>
class Child : Parent<T2>
{

  T2 d;

public:

  void set_d(T2 _d)
  {
    d = _d;
    //c = _d; // <<< this line generates error : 'c' was not declared
in this scope
    this->c = _d; // this access work  fine
    Parent<T2>::c = _d; // this access work  fine too
    set_c(_d); // there is no problem in finding 'set_c' in the scope
  }

};


int main(void)
{

  Child<int> the_child;

  the_child.set_d(5);

  return 0;
}
//////////////////////////////////////////

Command:
 g++ test.cpp -o test && ./test

g++ --version
g++ (GCC) 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)



More information about the Gcc-help mailing list