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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:36:03+00:00 2026-06-06T08:36:03+00:00

I want to store a function as a class member and call it inside

  • 0

I want to store a function as a class member and call it inside the class? Pretty much like a callback function. My class draw a document but every document must drawn differently. So I want to assign a function (written outside of the class) into one of the members of the class and then call it when I want to draw the document.

This function mostly is responsible for transforming objects according to each specific document.

Here is my class:

class CDocument
{
public:
    CDocument();
    ~CDocument();

    void *TransFunc();
}

void Transform()
{

}

int main()
    CDocument* Doc = new CDocument();
    Doc->TransFunc = Transform();
}

I know that this is probably simple question, but I couldn’t find the answer by googling or searching SO.

  • 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-06-06T08:36:04+00:00Added an answer on June 6, 2026 at 8:36 am

    I think, this is what you might want. Please get back to me if you have questions.

    class CDocument
    {
    public:
        CDocument():myTransFunc(NULL){}
        ~CDocument();
    
        typedef void (*TransFunc)();  // Defines a function pointer type pointing to a void function which doesn't take any parameter.
    
        TransFunc myTransFunc;  //  Actually defines a member variable of this type.
    
        void drawSomething()
        {
             if(myTransFunc)
                (*myTransFunc)();   // Uses the member variable to call a supplied function.
        }
    };
    
    void Transform()
    {
    
    }
    
    int main()
    {
        CDocument* Doc = new CDocument();
        Doc->myTransFunc = Transform;  // Assigns the member function pointer to an actual function.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For one class I want to store some function pointers to member functions of
I want to call a JavaScript function through PHP and store the returned value
I want to create a unit test for a member function of a class
I want to call the base class implementation of a virtual function using a
Let's say I want to store a group of function pointers in a List<(*func)>
I have a problem with the system function. I want to store the system
I want to store configuration information about my DLL in an XML file inside
Say want to store the following: typedef std::function<void(int)> MyFunctionDecl; ..in a collection: typedef std::vector<MyFunctionDecl>
I'd like to store some information about a class as class (static) variables. However,
I want to use boost::any to store heterogeneous function pointers. I get an exception

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.