Inheritence with teamples and member classes
Hey.
I ran into a problem recently, where code that i wrote on MSVC doesn't compile on GNU. Here is a sample:
Code:
#include <iostream.h>
template <typename T>
class Base
{
protected:
class Test
{
public:
T entry;
};
};
template <typename T>
class Child: public Base<T>
{
public:
void add();
};
template <typename T>
void Child<T>::add()
{
Base<T>::Test* test = new Base<T>::Test();
}
int main()
{
Child<int> child;
child.add();
return 0;
};
This code gives me the following error on GNU. Now to fix it i need to do this:
Code:
typename Base<T>::Test* test = new typename Base<T>::Test();
Now this is an annoying thing to have to write every single time i use it. Same with teh Base<T>:: which was not required on MSVC.
Sorry about a post about GNU, but i figured that differences in c++ compilers would be known around here. I like the MS way of doing it, saves a bunch of code that just complicates everything. Is there a way to get similar behavior on GNU compilers?
Thanks in advance,
Quell
Re: Inheritence with teamples and member classes
- You should know by now that iostream.h is completley non-standard. Also note that it's useless in your snippet's context; code/information that's irrelevant to the problem shouldn't be posted.
- Free-standing functions don't end in semicolons.
- Instead of being lazy and trying to find compiler extensions that suit you, write conformant code and use a simple typedef:
Code:
template <typename T>
class Child: public Base<T>
{
public:
void add();
typedef typename Base<T>::Test TestType; //Or whatever access is appropriate...
};
//...
template <typename T>
void Child<T>::add()
{
TestType* test = new TestType(); //Smart pointers are your friend(s).
}
- You should look into smart pointers.
Re: Inheritence with teamples and member classes
Thx alot for the response. Yeah i know the code is dirty as hell, but i guess typedefs are the only proper way to go here.