Google Groups Home
Help | Sign in
Inheritance from a Template class, problems accessing protected member
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
liam_herron  
View profile
 More options Sep 5, 9:21 pm
Newsgroups: comp.lang.c++
From: liam_herron <liam_her...@hotmail.com>
Date: Fri, 5 Sep 2008 06:21:55 -0700 (PDT)
Local: Fri, Sep 5 2008 9:21 pm
Subject: Inheritance from a Template class, problems accessing protected member

Here is my code:

#include <iostream>

template<typename T>
class RawPtr
{
protected:
   T*    pointee_;

public:
   RawPtr() : pointee_(0) {}
   RawPtr(T* pT) : pointee_(pT) {}

};

template<typename T>
class SmartPtr : public RawPtr<T>
{
public:
         SmartPtr() : RawPtr<T>() {}
explicit SmartPtr(T* pT) : RawPtr<T>(pT)
         {
            if (pointee_ != 0)
            {
               std::cout << "SmartPtr(T* pT): pointee_ non-null." <<
std::endl;
            }
         }

};

int main()
{
   double *pDoubleRaw = new double(1.0);
   SmartPtr<double> pDouble(pDoubleRaw);
   return 0;

}

On Linux (g++ (GCC) 3.4.6 20060404 (Red Hat 3.4.6-3)) I get the
following compilation error:

g++ testInheritanceWithTemplates.cpp
testInheritanceWithTemplates.cpp: In constructor
`SmartPtr<T>::SmartPtr(T*)':
testInheritanceWithTemplates.cpp:27: error: `pointee_' was not
declared in this scope

On Windows, this compiles fine.

Any ideas why this doesn't work on Linux?  Does the C++ standard think
that this is legal?

Regards,
Liam Herron


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
cch@srdgame  
View profile
 More options Sep 5, 11:10 pm
Newsgroups: comp.lang.c++
From: "cch@srdgame" <srdg...@gmail.com>
Date: Fri, 5 Sep 2008 15:10:17 +0000 (UTC)
Local: Fri, Sep 5 2008 11:10 pm
Subject: Re: Inheritance from a Template class, problems accessing protected member
于 Fri, 05 Sep 2008 06:21:55 -0700,liam_herron写到:

hi

Following is the code I tested.

#include <iostream>

template<typename T>
class RawPtr
{
protected:
   T*    pointee_;

public:
   RawPtr() : pointee_(0) {}
   RawPtr(T* pT) : pointee_(pT) {}

};

template<typename T>
class SmartPtr : public RawPtr<T>
{
public:
         SmartPtr() : RawPtr<T>() {}
explicit SmartPtr(T* pT) : RawPtr<T>(pT)
         {
            if (RawPtr<T>::pointee_ != 0)
            {
               std::cout << "SmartPtr(T* pT): pointee_ non-null." <<
std::endl;
            }
         }

};

int main()
{
        int n = 10;
        SmartPtr<int> IntPtr(&n);

}

--
cch@srdgame

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
liam_herron  
View profile
 More options Sep 6, 1:10 am
Newsgroups: comp.lang.c++
From: liam_herron <liam_her...@hotmail.com>
Date: Fri, 5 Sep 2008 10:10:04 -0700 (PDT)
Local: Sat, Sep 6 2008 1:10 am
Subject: Re: Inheritance from a Template class, problems accessing protected member
Yeah, I was able to do that as well but I am not sure where in the
standard it requires to specify the scope
of a templated parent class (Remember this is not the case for non-
templated inheritance).

If anyone can point me to where this is in the 2003 C++ standard, that
would be great.

Regards,
Liam


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jim Z. Shi  
View profile
 More options Sep 9, 10:44 am
Newsgroups: comp.lang.c++
From: "Jim Z. Shi" <ji...@cisco.com>
Date: Tue, 09 Sep 2008 10:44:21 +0800
Local: Tues, Sep 9 2008 10:44 am
Subject: Re: Inheritance from a Template class, problems accessing protected member

when the compiler gets here, it will encounter a name `RawPtr<T>', and
knows that that is a template class of type<T>. when a class is
inherited from a template class, compiler will *only* find the template
base class *name* in his symbol table, and will not check its definition
detail.
> {
> public:
>          SmartPtr() : RawPtr<T>() {}
> explicit SmartPtr(T* pT) : RawPtr<T>(pT)
>          {
>             if (pointee_ != 0)

The reason of your original code is the template specialization. when
you're defining your SmartPtr<T> here, compiler only know his base
template class name but not his details, so compiler doesn't know
SmartPtr<T> has a member pointee_ inherited from RawPtr<T>. But you
could explicitly point that out using:
        if(this->pointee_ != 0)
or
        if(RawPtr<T>::pointee_ != 0)
this will tell the compiler that there is a implicit rule that RawPtr<T>
has to contain a member named pointee_, and this rule will be checked
when you instantiate a SmartPtr<T> object.

what g++ has done here is right. because of template specialization,
compiler will not guarantee anything at complier time. think about this:

template<typename T> class base;

template<> class base<int>
{
protected:
   int i;

};

template<> class base<double>
{
protected:
   double foo()
     {
       return .0;
     }

};

template<typename T>
class derived : public base<T>
{
   int bar()
     {
       //i;  is there `i'?
       //foo(); or is there a foo()?
       return 0;
     }

};

in derived<T>::bar(), what should the compiler guarantee you? a `i' or
`foo()',  or even something else? -- nothing is good, so just guarantee
nothing here. you should cry for what you want exactly and compiler will
check it for you when it instantiates that.

cheers & HTH,
Jim


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google