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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:07:29+00:00 2026-05-14T20:07:29+00:00

The below code compiles with gcc v4.3.3 and the templated child class seems to

  • 0

The below code compiles with gcc v4.3.3 and the templated child class seems to be overriding a virtual function in the parent, but doesn’t that break the rule that you cannot have a virtual template function? Or is something else happening that I don’t understand?

class BaseClass
{
public:
  virtual void Func(int var)
  {
    std::cout<<"Base int "<<var<<std::endl;
  }

  virtual void Func(double var)
  {
    std::cout<<"Base double "<<var<<std::endl;
  }
};

template <class TT>
class TemplateClass : public BaseClass
{
public:
  using BaseClass::Func;
  virtual void Func(TT var)
  {
    std::cout<<"Child TT "<<var<<std::endl;
  }
};

int main(int argc, char **argv)
{
  BaseClass a;
  TemplateClass<int> b;
  BaseClass *c = new TemplateClass<int>;

  int intVar = 3;
  double doubleVar = 5.5;

  a.Func(intVar);
  a.Func(doubleVar);
  b.Func(intVar);
  b.Func(doubleVar);
  c->Func(intVar);
  c->Func(doubleVar);
  delete c;
}

This then outputs:

Base int 3
Base double 5.5
Child TT 3
Base double 5.5
Child TT 3
Base double 5.5

as I hoped, but I’m not sure why it works.

  • 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-14T20:07:29+00:00Added an answer on May 14, 2026 at 8:07 pm

    A class template may have virtual member functions.

    A member function template cannot be virtual. That is, the following is invalid:

    class C
    {
    public:
        template <typename T>
        virtual void f();
    };
    

    In addition, if a derived class has a member function template with the same name as a virtual function in a base class, it does not override the virtual function. So, if TemplateClass::Func had been a member function template, e.g.,

    template <typename T>
    void Func(T var) { /* ... */ }
    

    it would not have overridden BaseClass::Func.

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

Sidebar

Related Questions

This code below compiles fine with VS2010 but doesn't want to compile with gcc
The code below compiles fine with VS2010 but fails to compile with gcc 4.6.1:
Can we use namespaces like in below snippet? The code compiles in both gcc
I am getting a -Wunused-but-set-variable warning with GCC v4.6 with the code below: for
The code below demonstrates a behavior of gcc 4.6.2 that I can't account for.
Consider the code below. I don't understand why my GCC compiler does not try
Below code not work, but it's work fine for jsf1.2. Now the framework is
I have a doubt on basic C++ usage. The code below, compiled with gcc/LInux,
I have a sample code below. #include<iostream> template<typename T> class XYZ { private: T
I'm trying to compile some code (below) using gcc 4.6.2 on OSX 10.6.5. It's

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.