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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:18:00+00:00 2026-05-24T20:18:00+00:00

I guess my question should be silly but it is true that I have

  • 0

I guess my question should be silly but it is true that I have never seen a function pointer that is declared as virtual. Is there a reason for this?

Edit:

I should have said: Is it possible that the function it points to is specified as virtual?

  • 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-24T20:18:01+00:00Added an answer on May 24, 2026 at 8:18 pm

    Well, yes (and no).

    Ordinary function pointers cannot point to non-static member functions. They can only point to standalone functions, which is why for ordinary function pointers the matter of function virtuality does not even come into the picture.

    In order to point to member functions in C++ you need a pointer of special kind: one that has pointer-to-member-function type. A pointer of this type can point to non-virtual member functions as well and to virtual member functions. There are no special steps to take if you want to point to virtual member function. For example

    struct B { 
      virtual void foo() {} 
      void bar() {} 
    };
    
    ...
    B b;
    void (B::*pfunc)(); // declare a pointer to a member function
    
    pfunc = &B::foo; // make it point to `B::foo`
    (b.*pfunc)(); // calls `B::foo` for object `b`
    
    pfunc = &B::bar; // make it point to `B::bar`
    (b.*pfunc)(); // calls `B::bar` for object `b`
    

    However, when you make such pointer to point to a virtual member function, you have to keep in mind that it is does not really get tied to a specific version of that function in the class hierarchy. The decision about the specific function to call is made at the point of the call. For example

    // given the above `B`
    struct D : B { 
      virtual void foo() {} 
    };
    
    ...
    void (B::*pfoo)(); // declare a pointer to a member function
    pfoo = &B::foo; // and make it point to `B::foo`
    

    In the above example we made out pointer pfoo to point to B::foo. Or did we? In reality the pointer is not hard-linked to B::foo specifically. These two calls

    B b;
    D d;
    
    (b.*pfoo)();
    (d.*pfoo)();
    

    will call two different functions. The first one will call B::foo for object b, while the second one will call D::foo for object d, even though we used the same pointer value in both cases. This makes sense actually in many applications.

    Yet, in some low-level situation it would be useful to have a pointer that is hard-tied to a specific version of virtual function. I.e. it would be nice to have a pointer that would call B::foo for B subobject of object d when we do

    (d.*pfoo)();
    

    To achieve that we need to be able to specify whether we want to bind it early (at the point of initialization) or late (at the point of the call). Unfortunately, C++ language provides no such functionality.

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

Sidebar

Related Questions

Good evening, I have a quick question that should be pretty easy, but for
Maybe a silly question but where/how should I define default values for GORM domain
I guess the question title sums it up. Is there a time when it
I guess this question will sound familiar, but I am yet another programmer baffled
I guess my general question which I googled to no luck: Is there a
This is a simple question,I guess, but I couldn't figure it out. How do
Here's the main question I guess : Is there a .NET way to get
This is more of a general design question I guess... I have an Ajax
I know that the question has been asked before , but it's been two
I'm implementing a task queue with Amazon SQS ( but i guess the question

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.