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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:19:24+00:00 2026-05-25T00:19:24+00:00

I have 2 C++ Class questions: The 1st question is: How can I make

  • 0

I have 2 C++ Class questions:

The 1st question is: How can I make it so I can pass a class member function as a parameter in another function & how can I then run/call that function? And how can I do the same with a class static function. It maybe easier to understand my question by looking at this code:

class DebuggingManager
{
    string testLog;

    bool test1()
    {
         // run test & return whether it passed or failed
    }    

    static bool test2()
    {

    }

    // How can I call a member function?
    void catalogueTest( string testName, bool DebuggingManager::*nMemberFunction )
    {
        testLog += "Status of " + testName + ": " + ((*)nMemberFunction()) + "\n"; 
    }

    // How can I call a static function?
    void catalogueTest( string testName, bool DebuggingManager::*nStaticFunction )
    {
        testLog += "Status of " + testName + ": " + DebuggingManager::nStaticFunction() + "\n"; 
    }

    // how do I pass a member function or a static function as a parameter in another function 
    bool runTests()
    {
         catalogueTest( "Test of member functin", test1() );
         catalogueTest( "Test of static functin", test2() );
    }

};

The 2nd question is: Is it bad(or dangerous) practice to call a class member (or static) function indirectly like the above. I have a feeling this is really bad C++ practice?

EDIT: Implementing advice
Thanks for the reply, I have attempted to implement that advice, its alot to get my head around though, would this be correct?

    // I have a feeling that ParameterList is incorect, would I pass the implicit obj as a parameter or is it done automatically like in normal object function calls?
    typedef bool (DebuggingManager::*MemberPointerType)(ParameterList); 

    void catalogueTest( tstring testName, DebuggingManager* obj, MemberPointerType *nMemberFunction )
    {
        debugLog += _T("Status of ") + testName + _T(": ") + (obj->*nMemberFunction)() + _T("\r\n");
    }

    void catalogueStaticTest( tstring testName, bool DebuggingManager::nStaticFunction )
    {
        debugLog += _T("Status of ") + testName + _T(": ") + nStaticFunction + _T("\r\n");
    }
  • 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-25T00:19:26+00:00Added an answer on May 25, 2026 at 12:19 am

    Static member functions of classes are ultimately no different than regular functions. They’re really just syntactic sugar; the function is simply has a name that include Classname::.

    Non-static members are another matter altogether. There are two important things to remember about non-static member functions (NSMF).

    First, every non-static member function has access to the non-static members of the class that they are a member of. This is possible even though you can have two objects of the same class that happen to store different data. If you have two std::string objects, they each store different strings. Executing a find on one string can return a found result in one but not the other.

    This is because every NSMF has an implicit this pointer. this refers to, not merely a class, but the actual object upon which that NSMF operates. When you do this:

    std::string aString("data");
    aString.find("da");
    

    The find function takes a string argument, but it also gets aString as its this. Every time find looks for the members of its class, it will be looking at aString‘s data.

    So let’s look at your prospective call of an NSMF:

    ((*)nMemberFunction())
    

    Where is the object that it gets its this pointer from? Without an object, the NSMF could not access the non-static members of the object, since there is no object for it to find them in. This is not legal.

    So, rule #1 about NSMFs: You must call them with an actual instance of the class that the NSMF is a member of (or a derived class thereof). You cannot just take an NSMF pointer and call it like a function pointer; you have to call it on a live object of that type.

    Rule #2: the syntax for NSMF pointers is really ugly.

    To define a variable (or argument) named arg of NSMF pointer type, you do this:

    ReturnType (ClassName::*arg)(ParameterList);
    

    Where ReturnType is the return type of the function, ParameterList is the list of arguments taken by the function, and ClassName is the name of the class to which the NSMF pointer belongs.

    Given the ugliness, it is usually best to wrap it in a typedef:

    typedef ReturnType (ClassName::*MemberPointerType)(ParameterList);
    

    Thus creating the typedef MemberPointerType, which is a NSMF pointer.

    Given an object named object, which is of type ClassName, you would call the member pointer arg as follows:

    ReturnType value = (object.*arg)(Params);
    

    Where Params are the arguments you wish to pass. If object is a pointer to a ClassName instead of a reference or a value, then you use object->*arg instead.

    One more thing: you must use & to get the member pointer name. Unlike function pointers, NSMF pointers do not automatically convert to member pointers. You have to ask for them directly. So if ClassName has a member called Function that fit the above ReturnType and ParameterList, you would fill arg as follows:

    arg = &ClassName::Function;
    

    Rule #3: non-static member pointers are not pointers. Yes, they can be set to NULL (technically, they can be set to 0), but they are not the same thing as a pointer.

    Most real C and C++ compilers will allow you to cast a function pointer to a void* and back. The standards consider this undefined behavior, but it’s not-entirely-unknown to do this. You absolutely cannot do this with a NSMF pointer, on virtually all C++ compilers. Indeed, the sizeof(MemberPointerType) will likely not be the same size as void*.

    So, NSMF pointers are not regular pointers. Do not treat them as such.

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

Sidebar

Related Questions

I have questions about System.Threading.ThreadStart Class : where can I find its specifications (
I came across this class while reading a C# book and have some questions.
I have a Question class: class Question { public int QuestionNumber { get; set;
I have a class called Question that has a property called Type. Based on
I have a two classes: public class Question { public IList<Answer> Answers { get;
Basic C# syntax question: So I have this class public class BrandQuery<T> : Query<T>
I have a constructor question for C#. I have this class: public partial class
C# question (.net 3.5). I have a class, ImageData, that has a field ushort[,]
Actually my question is all in the title. Anyway: I have a class and
Simple question on best practice. Say I have: public class Product { public string

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.