Why doesn't this compile? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why doesn't this compile?

Class A { template<class T> virtual void describe(T) {std::cout<<sizeof(T)<<std::endl;} }

2nd Nov 2018, 2:41 PM
1704086_ibnul
1704086_ibnul - avatar
1 Answer
+ 3
First, class is written small and needs a semicolon at the end. Second, template functions cannot be virtual, since the vtable would be of infinite size. You can write your template on top of the class though, then for each type a new class is created, which has only one type for the vtable. template<class T> class A { //template<class T> virtual void describe(T) {std::cout<<sizeof(T)<<std::endl;} };
2nd Nov 2018, 3:15 PM
Matthias
Matthias - avatar