Pure virtual templated member function

Started by
9 comments, last by Oxyd 20 years ago
I''m trying to write this:
template  virtual symbol_t getSymbol (const std::string &symbol) = 0;  
But compiler complains:
invalid use of `virtual'' in template declaration of `virtual symbol_t Insanity::IModule::getSymbol(const std::string&)''  
How can I declare pure virtual templated function inside non-templated class? Oxyd --- - Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t. - Real programmers aren''t afraid of goto
Advertisement
template member functions can''t be virtual.
Nice... What should I do then? Declare it pure virtual non-templated and in derived class declare it as virtual templated? Or what?

Oxyd

---
- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto
I repeat: template member functions can''t be virtual.
Maybe it makes more sense said this way: virtual member functions can''t be templated.

If you want want runtime polymorphism, design the interface for runtime polymorphism. You can try returning a pointer to an interface, supporting a limited number of concrete types, use discriminated unions like the VARIANT type, etc.
The correct type of polymorphism using templates is ''template specialization'', not ''virtual functions''.

And also, you cant have templated functions in a non-templated class (hmmm... maybe if they are static, im not sure) - because it really doesnt make sense.

You always pass in an std::string, so what determines the return type. How would the compiler ever know what you are going to return??
Sorry psamty10, but why should templated (non virtual) members not be allowed in non template classes? It is allowed and makes sence in some cases.

[edited by - VolkerG on April 4, 2004 1:51:15 PM]
The function is called like object.getSymbol ("symbol");

What I want is to define different behaviour in each derived class and be templated... (Yes - now I know, it''s impossible to have virtual + template). But well... We''ll see.. Maybe I''ll return void * or something like this...

Oxyd

---
- Unreadable code is code written on a piece of paper, but not the one, in which the programmer is using a space in the place you don''t.
- Real programmers aren''t afraid of goto
quote:Original post by VolkerG
Sorry psamty10, but why should templated (non virtual) members not be allowed in non template classes? It is allowed and makes sence in some cases.

[edited by - VolkerG on April 4, 2004 1:51:15 PM]


Perhaps, as i said, I wasnt sure about this... what I wanted to know is how hed pass in the template argument with his function...

Oh and Oxyd.... I wouldnt use void*, but then again, you arent afraid of ''goto'' hehe
It sounds like maybe you are unsure if you can return the template typename from a function. You can, but the caller has to be specific and tell the compiler what type he is expecting.
MyType temp = getSymbol<MyType>("symbol");


[edited by - mfawcett on April 4, 2004 3:45:56 PM]
--Michael Fawcett
Exactly, and if youre doing that it makes sense to use separate functions...

This topic is closed to new replies.

Advertisement