ProgrammingHardMicrosoft/Google SDE MCQ · GATE CS

Which statement about virtual functions and templates in C++ is correct?

  1. A. A member function template can be virtual if it is also pure
  2. B. A class template's ordinary member functions can be virtual, but a member function template cannot
  3. C. Virtual functions are not allowed inside class templates
  4. D. A member function template becomes virtual when the base declares one with the same name

Correct answer: B. A class template's ordinary member functions can be virtual, but a member function template cannot

A vtable needs a fixed set of slots, but a member function template could be instantiated for unboundedly many types, so it cannot be virtual. Ordinary member functions of a class template can be virtual, since each instantiation has a fixed set.

More OOP with C++ and Java questions

Exams that ask this topic