Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What's wrong here? Accessing members of a templated base class

Reply
Thread Tools

What's wrong here? Accessing members of a templated base class

 
 
Pete
Guest
Posts: n/a
 
      07-22-2004
If anyone here can tell me what's going wrong in this snippet, I'd be much
obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...



template<typename T>
struct base
{
T m_x;
};

template<typename T>
struct derived : public base<T>
{
void bleh();
};

template<typename T>
void derived<T>::bleh()
{
T b = base<T>::m_x; // OK
T a = m_x; // error: `m_x' undeclared
}

int main()
{
derived<int> c;
c.bleh();
}

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-22-2004

"Pete" <(E-Mail Removed)> wrote in message
news:AsPLc.48097$(E-Mail Removed). uk...
> If anyone here can tell me what's going wrong in this snippet, I'd be much
> obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...
>
>
>
> template<typename T>
> struct base
> {
> T m_x;
> };
>
> template<typename T>
> struct derived : public base<T>
> {
> void bleh();
> };
>
> template<typename T>
> void derived<T>::bleh()
> {
> T b = base<T>::m_x; // OK
> T a = m_x; // error: `m_x' undeclared
> }
>


It's because 3.4 implements two phase look up correctly and therefore does
not examine the base class when looking up a unqualified dependent name.

Either base<T>::m_x or this->m_x will fix the problem.

John


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      07-22-2004
Pete wrote in news:AsPLc.48097$(E-Mail Removed). uk in
comp.lang.c++:

> If anyone here can tell me what's going wrong in this snippet, I'd be
> much obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4.
> Thanks...
>
>


The name m_x from the base 'base< T >' is dependant on the template
argument 'T', you need to tell a conforming compiler (g++ 3.4 in this
case) that 'm_x' is in 'base< T >' otherwise it will assume its a global.

>
> template<typename T>
> struct base
> {
> T m_x;
> };
>
> template<typename T>
> struct derived : public base<T>
> {


Fix 1:

using base< T >::m_x;

> void bleh();
> };
>
> template<typename T>
> void derived<T>::bleh()
> {


Fix 2:

> T b = base<T>::m_x; // OK
> T a = m_x; // error: `m_x' undeclared


Fix 3:

T c = this->m_x;

> }
>


Note that Fix 1 puts a requirement on derived< T > that the
instansiated specialization for 'base< T >' contains a non-type
member m_x, Fix 2 and 3 only make this requirement if
derived< T >::bleh() is instantiated.

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Sharad Kala
Guest
Posts: n/a
 
      07-22-2004

"Pete" <(E-Mail Removed)> wrote in message
news:AsPLc.48097$(E-Mail Removed). uk...
> If anyone here can tell me what's going wrong in this snippet, I'd be much
> obliged. It works under g++ 3.3 but not the (fussier) g++ 3.4. Thanks...
>
>
>
> template<typename T>
> struct base
> {
> T m_x;
> };
>
> template<typename T>
> struct derived : public base<T>
> {
> void bleh();
> };
>
> template<typename T>
> void derived<T>::bleh()
> {
> T b = base<T>::m_x; // OK
> T a = m_x; // error: `m_x' undeclared
> }
>
> int main()
> {
> derived<int> c;
> c.bleh();
> }


That's two phase name lookup. I just checked our FAQ and saw that it is
actually mentioned there.
http://www.parashift.com/c++-faq-lit....html#faq-34.1
7
If you want to know even better then read Part II (Chapters 9 and 10) of C++
Templates (Josuttis/Vandevoorde). They explain it quite well in their book.

-Sharad






 
Reply With Quote
 
Pete
Guest
Posts: n/a
 
      07-22-2004
Sharad Kala wrote:

> That's two phase name lookup. I just checked our FAQ and saw that it is
> actually mentioned there.
>

http://www.parashift.com/c++-faq-lit....html#faq-34.1
> 7
> If you want to know even better then read Part II (Chapters 9 and 10) of
> C++ Templates (Josuttis/Vandevoorde). They explain it quite well in their
> book.
>


Ah, the c++-faq. Nice explanation... quite an eye-opener. I'll have to
change a load of code because of that one. Bah.

Thanks for the help, everyone.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing derived class members from base class Bhawna C++ 7 08-26-2008 11:03 AM
Defining templated member function outside templated class chhenning C++ 5 02-13-2008 07:36 PM
accessing base class members when base is template flopbucket C++ 2 06-23-2006 01:40 AM
accessing members of a templated base class John Harrison C++ 18 08-10-2003 11:41 PM
Accessing data members from templated copy constructor Alexandre Tolmos C++ 1 08-08-2003 01:21 PM



Advertisments