Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7640721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:44:28+00:00 2026-05-31T08:44:28+00:00

I know there are few threads about this topic. But what really confused me

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:44:29+00:00Added an answer on May 31, 2026 at 8:44 am

    You can’t have a virtual function template in a class — this 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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there have been a few threads on this before, but I have
I know there's tons of threads about this. And I read a few of
First of all, I know there are a few other StackOverflow questions about this
So I know there is a few templating engines out there, but I thought
I know how to use locks in my app, but there still few things
Anyone know? I found a few answers, but there were too complex and going
I'm learning about POSIX threads right now, but I guess this is just a
I know, I know. There are a million threads everywhere talking about problems with
Questions about threads are in no shortage, I know, but I can't seem to
This is similar to a few other threads i have found, but I haven't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.