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 7071667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:56+00:00 2026-05-28T05:42:56+00:00

Once before, I was certain that you couldn’t do this, but the other day

  • 0

Once before, I was certain that you couldn’t do this, but the other day I was playing around with some code and it seemed to compile and work. I just want to verify that I am not just getting lucky. Can a template class have a pure virtual function – which I guess would also mean just plain virtual methods would be valid as well for the destructor?

template <typename WordType> class DataSource
{
public:
    DataSource();
    DataSource(DataSource const& other);
    virtual ~DataSource();

    virtual void Put(
        WordType const* const data,
        unsigned int const wordCount) = 0;
}

I’ve tried looking it up online and all that I’ve been able to find is that you cannot have a virtual method (pure or otherwise) in a normal class such as this:

class DataSource
{
public:
    DataSource();
    DataSource(DataSource const& other);
    virtual ~DataSource();

    template <typename WordType>
    virtual void Put(
        WordType const* const data,
        unsigned int const wordCount) = 0;
}

And that this is due to the imposibility of managing a virtual table to reference all the different types of possible types this method would be instanciated with.

However, when it came to a virtual member function of a template class, it seems to be different because the whole class itself is “created” via the template parameter when the template class variable is instanciated. At this point, the virtual method is just like any other virual method of a class due to the “find-and-replace” nature of templates.

Anyway, stating the question again in case it got lost in there:
Are virtual (pure and/or normal) virtual functions allowed within a tempate class?

  • 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-28T05:42:56+00:00Added an answer on May 28, 2026 at 5:42 am

    A class template can indeed contain virtual or pure virtual functions. This was employed by Andrei Alexandresu in “Modern C++ Design” to implement the visitor pattern using templates and type lists. You can see the code here in his Loki library if you’re interested.

    With most standard C++ implementations, this is fine, because when the template is instantiated the virtual function ends up being one single function. Consequently, the number of slots needed in the vtable can be known within the translation unit, so a vtable can be generated.

    As you mentioned, you cannot have a virtual template member function because the number of vtable slots wouldn’t be known within the translation unit.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know you can do this, because I've seen it done once before, but
I've done this before once, I'm trying to replicate what I did so far
I have a website that needs to perform a certain backend function once per
I often want to trigger a certain function just once, but I need to
I would like to mention before continuing that I have looked at other questions
Once again a very beginner-ish question, but here I go: I would like to
This is really annoying me as I have done it before, about a year
I can't see anything on here but I do remember being told that If
I'm actually writing about the same program as before, but I feel like I've
Is there a way to run code BEFORE model retrieval? I know about the

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.