473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

template class derived from non-template base class

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
Jul 19 '05 #1
4 10014
"Matt Graham" <mg*****@cymedix.com> wrote...
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().
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.

Is deriving a template class from a class without templates forbidden
in C++?
No.
Is it possibly a compiler support issue?
No, it shouldn't be. If it's so, change your compiler.
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.


That's of no real consequence.

Victor
Jul 19 '05 #2
Matt Graham wrote:
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


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.

Jul 19 '05 #3

"Matt Graham" <mg*****@cymedix.com> wrote in message
news:28**************************@posting.google.c om...
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() }
I guess you forgot the semicolon here.
protected:
virtual void y();
};

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

class D< A > d1;
I assume that you meant

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++?
No, this is perfectly legal. In principle your code should run just fine.

Is it possibly a compiler support issue?
What compiler are you using and can you post some more code.
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


Chris
Jul 19 '05 #4
"Chris Theis" <Ch*************@nospam.cern.ch> wrote in message news:<bf**********@sunnews.cern.ch>...
"Matt Graham" <mg*****@cymedix.com> wrote in message
news:28**************************@posting.google.c om...
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++?


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

Is it possibly a compiler support issue?

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

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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sebastian Ferreyra | last post by:
Hi, I'm writng a control derived from UserControl (let's call this control MyControl) that itself is meant to be derived further times. MyControl hosts a public collection of specialized...
2
by: Keith B via SQLMonster.com | last post by:
Hi! I want to return a derived table along with 4 simple tables in a stored procedure as follows: Input parameter: @FtNum (==Order Number, selects one Order and all associated data)...
3
by: Riccardo Maffei | last post by:
Hail to all. I have a problem that I hope could help me to resolve. I should write a small program that calculates the derived symbolic of a given function under form of bootlace. Where could I...
9
by: Bruce | last post by:
How can I create a method that will take any object derived from a base class as a reference parameter? Something like this: void DestroyObject(ref BaseObject obj) { obj.Dispose(); obj =...
3
by: dbuchanan | last post by:
Can inherited code call derived code? If so how. I have identical 'generic' code that I am repeating again and again in several derived form because I don't know how to get inherited code to call...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
7
by: Ian C | last post by:
Hello all Apologies if this is a dumb question, but this is driving me nuts, and I'm really an old procedural programmer so not sure if it's me or the compiler at fault. I can perhaps guess :-)...
3
by: Andrew | last post by:
Hi All I wonder if anyone can help me with this. The scenario is that I have a pair of related tables. One contains record labels, the other contains contact names at those labels. In the...
15
by: iu2 | last post by:
Hi all, can someone help me with this? I'm trying to shorthen some logging function in our project, i.e., instead of LogString("...); use a more convenient log() << "String here " <<...
4
by: jerstlouis | last post by:
I'm hoping to generate some interest in the Ecere SDK which is the result of many years of software development. With the Ecere SDK, you can: Build Cross Platform GUI Applications: Develop...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.