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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:42:10+00:00 2026-05-23T04:42:10+00:00

I want to call a member function which is virtual (inheritance is used in

  • 0

I want to call a member function which is virtual (inheritance is used in most places to keep things simple), but I want to force calling it using non-virtual dispatch sometimes, in performance critical places, and in such places the exact type is known compile time. I do this for performance reasons, on a platform where virtual call performance is bad. For most functionality the overhead of virtual functions is fine, but for a few it is not. I would like to avoid duplicating all functions as both virtual and non-virtual.

Example:

class Interface
{
  public:
  virtual void Do(){}   
};

class Implementation: public Interface
{
  public:
  virtual void Do(){}   
};


void DoIt(Interface &func)
{
  func.Do();
};

int main()
{
  Implementation a;
  DoIt(a);
  // can DoIt be constructed as a template to avoid virtual dispatch?
  return 0;
}
  • 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-23T04:42:11+00:00Added an answer on May 23, 2026 at 4:42 am

    If you know the exact type you can do it as:

    template <typename StaticType>
    void DoIt(Interface &func)
    {
      static_cast<StaticType&>(func).StaticType::Do();
    };
    

    Where you need to manually downcast to the type you need (static_cast is fine if you do know the type). Then you need to qualify the method call, do disable dynamic dispatch.

    struct DerivedType : Interface {
        virtual void Do() { std::cout << "Derived::Do" << std::endl; }   
    };
    struct MostDerived : DerivedType {
        virtual void Do() { std::cout << "MostDerived::Do" << std::endl; }
    };
    void processDerived( Interface & iface ) {
       DoIt<DerivedType>( iface );
    }
    int main() {
       MostDerived obj;
       DoIt<Derived>( obj );    // Will call Derived::Do
    }
    

    Note that using the qualified name will disable dynamic dispatch, and that means that it will not be dispatched to the runtime type of the object, but to the type that you tell it to call.

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

Sidebar

Related Questions

I want to call a function (which calculates my version number) when my NSIS
I want to call the base class implementation of a virtual function using a
I want to print state of my objects through toString member function, but I
I want to call a function from a .NET DLL (coded in C#) from
I want to call a web service, but I won't know the url till
I want to call ShowDialog() when a keyboard hook event is triggered, but I'm
I want to call a c# function from my javascript function. I have a
If I try to call a member function of a member of the base
Im about to figure out how to call a function with one member of
I want to make a declaration of an external function,but I can't do it.

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.