Skip to main content
gamedev.net
🔒 Locked

virtual teplate

Started by Niko1Dude 4 replies 550+ views
Original Post
Niko1Dude
Niko1Dude
In C++, can I declare a virtual template class member function? Thanks!
RDragon1
RDragon1
Look into how virtual functions work, and look into how templates work, and see if you can figure it out... Why do you THINK it can't work?
Polymorphic OOP
Polymorphic OOP
Because virtuality is generally implemented in C++ with vtables. The concept of a template is that for each combination of template arguments used, a new, entirely separate function is created. Because of that, in order for it to work with a vtable, the compiler/linker would have to know all of the different instantiations that will possibly be used via the base, make appropriate vtable entries for each, and be sure that all translation units index it correctly -- not to mention that it pretty much completely gets rid of the possibility to use the function from other executables which would link to it if they were to use unexpected instantiations.

Instead of making the template virtual, just make non-template virtual functions for the stuff that you need to be virtual, then make a non-virtual template which calls the appropriate virtual functions.
Niko1Dude
Niko1Dude
I think I got it. The key is that templates get instantiated in compile time while virtuality is run time thing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.