Inheritance from a Template class, problems accessing protectedmember

L

liam_herron

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
 
C

cch@srdgame

于 Fri, 05 Sep 2008 06:21:55 -0700,liam_herron写到:
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


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

liam_herron

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top