Archived

This topic is now archived and is closed to further replies.

Pure virtual templated member function

This topic is 5000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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??

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
You can do C++ generic programming with the templated version.
Consider boost's lexical_cast.

You could also return a base ISymbol* and use co-variant return types in the derived classes.

[edited by - Magmai Kai Holmlor on April 4, 2004 10:22:20 PM]

Share this post


Link to post
Share on other sites