bug-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

protected member variable visibility through templates in 3.4.0


From: Chris
Subject: protected member variable visibility through templates in 3.4.0
Date: Sat, 21 Aug 2004 18:42:31 -0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7b) Gecko/20040316

Hi,

I might have found a bug -- I'm not a C++ language expert, so I'm not sure. This is maybe OK in the stable build -- 3.2.3 -- or in the newer version --- 3.4.1.

I've included an example showing the problem, and the output I get. Essentially, a derived template can't see protected member variables in or through a parent template.

This doesn't happen on slightly earlier versions of the compiler (3.3.2, for example).

I should say that I built this compiler from source, so it could be a problem with my build, but I saw the same thing on two platforms (Sparc and i386).

I just checked and there is a newer version (3.4.1) available. Maybe this is OBE.

Thanks,

Chris Arena
Portsmouth, RI
401 293 0449
#include <stdio.h>

class Base
{
 protected:

  //
  // this should be accessible to derived classes; it is for non-template
  // classes, but isn't for template classes
  //
  int first_Var;

 public:

  Base()
    :
    first_Var(0)
  {
  }
};

//
// Templates with troubles
//
template <class T> 
class TDerived1 : public Base
{
 protected:

  //
  // this should be accessible to derived classes; it is for non-template
  // classes, but isn't for template classes
  //
  T second_Var; // this should be accessible to derived classes

 public:
  TDerived1()
    :
    second_Var( 0 )
  {
    //
    // The compiler won't complain about this line.
    //
    printf("The value of first_Var is %d\n", first_Var );
  }
};

template <class T>
class TDerived2 : public TDerived1<T>
{
  T third_Var;

 public:

  TDerived2()
    :
    third_Var( 0 )
  {
    //
    // The compiler will complain about this line...
    //
    printf("The value of first_Var is %d\n", first_Var );

    //
    // The compiler won't complain about this line, but the
    // additional qualification shouldn't be necessary (AFAIK).
    // Am I wrong about that?
    //
    printf("The value of first_Var is %d\n", TDerived1<T>::first_Var );

    //
    // The compiler will complain about this line...
    //
    printf("The value of second_Var is %d\n", second_Var);

    //
    // The compiler won't complain about this line, but the
    // additional qualification shouldn't be necessary (AFAIK).
    // Am I wrong about that?
    //
    printf("The value of second_Var is %d\n", TDerived1<T>::second_Var);

    //
    // The compiler better not complain about this line!!
    //
    printf("The value of third_Var is %d\n", third_Var );

  }
};

//
// No template structural equivalent
//
class Derived1 : public Base
{
 protected:

  int _2nd_Var;

 public:

  Derived1()
    :
    _2nd_Var( 0 )
  {
    printf("The value of first_Var is %d\n", first_Var );
  }
};

class Derived2 : public Derived1
{
 protected:

  int _3rd_Var;

 public:

  Derived2()
    :
    _3rd_Var( 0 )
  {
    printf("The value of _2nd_Var is %d\n", _2nd_Var);

    printf("The value of _3rd_Var is %d\n", _3rd_Var );

    first_Var = 123;

    printf("The value of first_Var is %d\n", first_Var );
  }
};
//
// For the compilation of the above, I get the following errors:
/*

address@hidden junk]$ g++ -g junk.cpp
junk.cpp: In constructor `TDerived2<T>::TDerived2()':
junk.cpp:62: error: `first_Var' undeclared (first use this function)
junk.cpp:62: error: (Each undeclared identifier is reported only once for each 
function it appears in.)
junk.cpp:74: error: `second_Var' undeclared (first use this function)

 */


int main(int argc, char ** argv)
{
  Base           base;
  Derived1       derived1;
  Derived2       derived2;
  TDerived1<int> tDerived1;
  TDerived2<int> tDerived2;

  return 0;
}


reply via email to

[Prev in Thread] Current Thread [Next in Thread]