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: Mirko Predosin
Subject: protected member variable visibility through templates in 3.4.0
Date: Mon, 25 Jul 2005 15:38:00 -0700
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

I had a similar piece of code attempting to create a derived template
class from a base template class.  Upon searching from similar issues I
found an email posted from Chris on Sat, 21 Aug 2004 18:42:31 -0400.
The issues seems to be still unresolved using more recent versions of gcc.

I have confirmed the bug occurs in 3.4.3 and gcc 4.0.1.  The code does
compile using gcc 3.3.6.

Source code:

#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 );
                       }
};


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

       return 0;
}

Output from gcc 3.4.3:

DerivedTemplates.cpp: In constructor `TDerived2<T>::TDerived2()':
DerivedTemplates.cpp:62: error: `first_Var' undeclared (first use this
function)
DerivedTemplates.cpp:62: error: (Each undeclared identifier is reported
only once for each function it appears in.)
DerivedTemplates.cpp:74: error: `second_Var' undeclared (first use this
function)

Output from gcc 4.0.1:

DerivedTemplates.cpp: In constructor âTDerived2<T>::TDerived2()â:
DerivedTemplates.cpp:62: error: âfirst_Varâ was not declared in this scope
DerivedTemplates.cpp:74: error: âsecond_Varâ was not declared in this scope

Output from binary produced using gcc 3.3.6:

./a.out
The value of first_Var is 0
The value of first_Var is 0
The value of _2nd_Var is 0
The value of _3rd_Var is 0
The value of first_Var is 123
The value of first_Var is 0
The value of first_Var is 0
The value of first_Var is 0
The value of first_Var is 0
The value of second_Var is 0
The value of second_Var is 0
The value of third_Var is 0

-- Mirko





reply via email to

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