template class derived from non-template base class

template class derived from non-template base class

Post by mgraha » Sat, 19 Jul 2003 00:21:17


ok, I have 2 classes roughly like these 2:

class A; // various data classes to be used in D

class B {
public:
void x() { y() }
protected:
virtual void y();
};

template < class T >
class D : public B {
public:
void z();
protected:
virtual void y();
};

class D< A > d1;

Now, whenever I call d1.x(), it runs B::y() instead of D::y().

Is deriving a template class from a class without templates forbidden
in C++?
Is it possibly a compiler support issue?

My reason for doing this was to keep the size from getting so big as
I'm using the D class with many different classes in place of A on a
device with limited memory and storage.

Thanks,
Matt Graham
 
 
 

template class derived from non-template base class

Post by Victor Baz » Sat, 19 Jul 2003 00:53:29

"Matt Graham" < XXXX@XXXXX.COM > wrote...

Posting non-compilable code does not get you anywhere. Here
is what I came up with (after fixing your code and adding the
necessary framework):

---------------------------------
#include <iostream>
using namespace std;

class B {
public:
void x() { y(); }
protected:
virtual void y() { cout << "B::y\n"; }
};

template < class T >
class D : public B {
public:
void z();
protected:
virtual void y() { cout << "D::y\n"; }
};

int main()
{
D<int> d1;
d1.x();

return 0;
}
---------------------------------
The output of this code is
D::y

So, either you forgot to actually declare 'y' virtual in
your original code, or there is something else. Please post
real code next time.


No.


No, it shouldn't be. If it's so, change your compiler.


That's of no real consequence.

Victor

 
 
 

template class derived from non-template base class

Post by Gianni Mar » Sat, 19 Jul 2003 01:00:47


I suggest that next time you post an actual working example of the code.

#include <iostream>

class A {
};

class B {
public:
void x() { y(); }
protected:
virtual void y() { std::cout << "B::y\n"; }
};



template < class T >
class D : public B {
public:
void z();
protected:
virtual void y() { std::cout << "D<>::y\n"; }
};

class D< A > d1;


int main()
{
d1.x();
}


anyhow:

The output for gcc 3.3 is:
D<>::y

which seems correct to me and reading the rest of your post indicates
that this is not what you got but what you expect.

Sounds like compiler trubbel.
 
 
 

template class derived from non-template base class

Post by Chris Thei » Sat, 19 Jul 2003 01:41:37


I guess you forgot the semicolon here.


I assume that you meant

D<A> d1;


No, this is perfectly legal. In principle your code should run just fine.


What compiler are you using and can you post some more code.


Chris
 
 
 

template class derived from non-template base class

Post by mgraha » Wed, 23 Jul 2003 06:02:05


Thanks to those who responded. I decided it must be a compiler issue
and ended up trying a few work arounds before finding one that I
should have tried some time ago.

Sorry about posting code that couldn't be compiled. I'll note that for
next time and not be so concerned about brievity. My actual code
compiled fine and ran fine right up until it called the base class
virtual member function instead of the derived class virtual member.

The compiler I am using is CodeWarrior 9 for PalmOS. I guess I'll
check out a related newsgroup to see if that's a known problem.

Thanks
Matt