Re: template classes: inheritance problem
On 5 Feb., 15:04, Ulrich Eckhardt <eckha...@satorlaser.com> wrote:
vl106 wrote:
template<int T = 0>
class Derived : public Base {
public:
virtual void foo() { /* default does nothing */ }
// private:
// int data_; // <== PROBLEM #2: not seen in Derived<1>
};
Well, you have just found out that specialisations of templates can
completely differ from the unspecialised ones. That also means that nothing
is "carried over" from the base version by default, neither base classes
nor members.
Note: you could have written this as well:
template<int T = 0> class Derived;
template<> class Derived<1> { ... };
IOW, you don't have to define a general Derived<T>!
Seeing your other posting here, I have a question: why do you insist on
using templates? AFAICT, they just don't fit your goals of creating
different objects at runtime, though I'm guessing a bit what those goals
could actually be.
Thanks for your answers. I think templates are a good solution to my
problem:
[PSEUDOCODE]
void receive_message(int messageId, char* messageData) {
Base& newMessage = Factory::create(messageId, messageData);
newMessage.process();
}
The receive_message function will stay pretty stable. If new kind of
message
is "invented" only a new specialication has to be created.
What really differs is the process method. Each specialization (based
on messageId) has ist own (and different) behaviour.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]