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

The Archive Base Latest Questions

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

I’ve two functions MultiplyVerison1(T x, T y); // in class A MultiplyVersion1(T x, T

  • 0

I’ve two functions

MultiplyVerison1(T x, T y); // in class A
MultiplyVersion1(T x, T y); // in class B

Above functions are in separate non-template classes.

Now, as part of refactoring I’m trying to create a base class of A and B and creating a pure virtual MultiplyVersion1 but a templte function cannot be marked virtual.

So, how can we achieve the same with template functions?

  • 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-28T18:03:43+00:00Added an answer on May 28, 2026 at 6:03 pm

    You can’t. There’s no way to call a function template in a derived class through a pointer-to-base, that’s what “function templates cannot be virtual” means.

    You can think of this as being because it’s the call that triggers the instantiation of the function template with a particular type T — if you call it with int, but the dynamic type of the object you’re calling it on isn’t known until runtime (whether it’s A or B or something else), then there’s no way for the compiler to know that it needs to instantiate A::MultiplyVersion1<int> or B::MultiplyVersion1<int> or something else. Actually there’s more to it than that, but I think that’s enough.

    You can bodge around particular cases, but you won’t get the full effect of a virtual function. Something like:

    struct Base {
        template <typename T>
        void MultiplyVersion1(const T &x, const T &y) {
            A *athis = dynamic_cast<A*>(this);
            if (athis) {
                athis->MultiplyVersion1(x,y);
            } else {
                B *bthis = dynamic_cast<B*>(this);
                if (bthis) {
                    bthis->MultiplyVersion1(x,y);
                } else {
                    throw std::logic_error();
                }
            }
        }
        virtual ~Base() {}
    };
    

    Now when you call MultiplyVersion1<int> via a pointer-to-base, both A::MultiplyVersion1<int> and B::MutiplyVersion1<int> are instantiated. But of course you can’t easily add new derived classes, which is a serious restriction.

    You could also re-consider whether you really need dynamic polymorphism at all, but that depends entirely on how you’re planning to use that base class. You seem to have done OK without it so far.

    If all you want from the base class is code re-use for some other functions, then you don’t need dynamic polymorphism. Leave MultiplyVersion1 out of the base class entirely (and maybe don’t inherit publicly from the Base, instead inherit privately and bring in the functions you want to re-use with using statements). If the functions you want to define for re-use call MultiplyVersion1, then consider simulated dynamic binding via CRTP:

    #include <iostream>
    
    template <typename Derived>
    struct Base {
        template <typename T>
        void MultiplyVersion2(const T &x, const T &y) {
            static_cast<Derived&>(*this).MultiplyVersion1(x + 1, y + 1);
        }
    };
    
    struct A : private Base<A> {
        friend class Base;
        template <typename T> void MultiplyVersion1(T x, T y) {
            std::cout << x*y << "\n"; 
        }
        using Base::MultiplyVersion2;
    };
    
    struct B : private Base<B> {
        friend class Base;
        template <typename T> void MultiplyVersion1(T x, T y) {
            std::cout << x << " * " << y << " = " << x*y << "\n"; 
        }
        using Base::MultiplyVersion2;
    };
    
    int main() {
        A a;
        a.MultiplyVersion2(1,2);
        B b;
        b.MultiplyVersion2(1,2);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put

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.