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

  • Home
  • SEARCH
  • 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 6379147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:06:00+00:00 2026-05-25T02:06:00+00:00

See the code below – I want to know how to, or if it

  • 0

See the code below – I want to know how to, or if it is even possible to, write the body of A::DoThisOrThat()

class A
{
  void DoThis( void ) { // any function body you like... }
  void DoThat( void ) { // any function body you like... }

  void DoThisOrThat( const bool doThis );
};

void A::DoThisOrThat( const bool doThis )
{
    void (*pMemberFunction)( void );
    pMemberFunction = doThis? &A::DoThis : &A::DoThat;

    (*pMemberFunction)();    // member function invoked controlled by parameter
}

All three lines of this function may need rewriting to correctly scope the member function. As written, I get a calling convention conflict on the second line where I assign pMemberFunction. I cannot lay my hands on a simple example clarifying this – all the examples easily found declare a member variable to hold the member function pointer. Can I not just create a local variable to do the same?

  • 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-25T02:06:01+00:00Added an answer on May 25, 2026 at 2:06 am

    I might be missing something, but isn’t it much easier and simpler to do this?

    void A::DoThisOrThat(bool doThis) 
    { 
        if(doThis) { DoThis(); }
        else       { DoThat(); }
    }
    

    Assuming you really want to go the function pointer route, realize that pointer to member functions are completely different beasts from pointers to non-member functions. One of the ways they are different is that you need to provide an object for the function to invoke upon. The syntax is as follows:

    void A::DoThisOrThat(bool doThis) 
    { 
        void (A::*pMemberFunction)();
        pMemberFunction = doThis ? &A::DoThis : &A::DoThat;
        (this->*pMemberFunction)();
    } 
    

    Note that when declaring a pointer to member function, you have to also specify the class (note the A::*) and the object (note the this->*). Also remember that when forming pointers to members you have to fully qualify the function name, even within the class, and use the ampersand (e.g. &A::DoThis, which you’ve correctly done).

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

Sidebar

Related Questions

Please see code below. The destructors are never called. Anyone know why and how
I have problem with session and query execution,please see code below. class A implements
I have problem with session and query execution, please see code below. class A
See code below, for some reason it only works when I put a breakpoint
Can't seem to figure this out, see code below. Trying to make a GET
How come unserialize isn't restoring my array? See code below.. // prints a:1:{s:8:txn_type;s:32:recurring_payment_profile_cancel;} echo
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
I have a table defined (see code snippet below). How can I add a
I am using vb.net code. I have grid view, please see the code below:
I am having a problem using the signedCMS.decode routine. See the code below. 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.