6

I know there are few threads about this topic. But what really confused me is the result I got is different from what everyone is saying.

Look this code below (compiled using GCC441):

#include <iostream>

using namespace std;

template<class T>
class A {
  public:
    A(T &t) : _a(t) {};
    virtual ~A() { cout << "Dtor-A" << endl;};
    virtual void print () { cout << "A: " << _a << endl; }
    T _a;
};

class B : public A<int> {
  public:
    B(int t) : A<int>(t) {}
    ~B() { cout << "Dtor-B" << endl;};
    void print() { cout << "B: " << endl; }
};

int main() {
  B b(2);

  A<int> *a = &b;
  a->print();

  A<int> *a2 = new B(4);
  a2->print();

  delete a2;
}

The result is:

B: 
B: 
Dtor-B
Dtor-A
Dtor-B
Dtor-A

If virtual function is not allowed in template class, why did I get this result?

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
Orunner
  • 368
  • 3
  • 12
  • I guess there is a difference between template virtual function and virtual function template class. Am I right? – Orunner Mar 13 '12 at 12:39
  • did you read this? http://stackoverflow.com/questions/2354210/can-a-member-function-template-be-virtual – Stefan Birladeanu Mar 13 '12 at 12:41
  • 1
    @Orunner: Vandevoorde/Josuttis also recommend to put the "template" after what it refers to: "class template" -> `template<> class X;`, "function template" -> `template<> void foo();`, "member function template" -> `class X { template<>void foo(); };`, "class template member function template" -> `template<> class X { template<> void foo(); };`; this way you stay consistent and unambiguous. – Sebastian Mach Mar 13 '12 at 13:12
  • @phresnel: Strictly speaking, that's because "class" is _not_ what "template" refers to; rather, in "class template", "template" is what "class" refers to. "Template" is the noun and "class" is the adjectival noun describing what kind of template it is. :) A red template, a cookie template, a function template. Thinking in this manner also, in my experience, helps language newcomers to more easily grasp the various concepts of template instantiation than does thinking of a "template function", like some kind of sibling variant of a "normal function". – Lightness Races in Orbit Mar 13 '12 at 13:41

2 Answers2

13

You can't have a virtual function template in a classthis would be meaningless — but a virtual function in a class template is fine.

Once that class template Foo is instantiated, the resulting class Foo<T> has virtual functions that can be used as if they were part of any other type.

Let's examine the difference between the two:

Not OK

struct Foo {
   template <typename T>
   virtual void bar() {}      // Foo::bar<T> - prohibited, as marked "virtual"
};

// Impossible to resolve calls to Foo::bar<T>() at runtime, since
// such calls may require template instantiation, which may only occur
// at compile-time.

OK

template <typename T>
struct Foo {
   virtual void bar() {}      // Foo<T>::bar
};

// If Foo<T> is used in the program, then it will exist, in entirety.
// Foo<T>::bar() may thus be used as noraml.
Community
  • 1
  • 1
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
0

The behaviour is correct. The virtual member function template is what you mention.

Jurlie
  • 1,004
  • 10
  • 27