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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:32:30+00:00 2026-05-21T20:32:30+00:00

I’m working on finishing up my server for my first iPhone application, and I

  • 0

I’m working on finishing up my server for my first iPhone application, and I want to implement a simple little feature.

I would like to run a function (perhaps method as well), if another function returns a certain value after a certain waiting period. Fairly simple concept…. right?

Here’s my basic foundation.

template <typename T,class TYP>
struct funcpar{
 T (*function)(TYP);
 TYP parameter;
 funcpar(T (*func)(TYP),TYP param);
 funcpar& operator=(const funcpar& fp);
}; 

The goal here is to be able to call funcpar::function(funcpar::parameter) to run the stored function and parameter, and not have to worry about anything else…

When I attempted to use a void* parameter instead of the template, I couldn’t copy the memory as an object (because I didn’t know what the end object was going to be, or the beginning for that matter) and when I tried multiple timers, every single object’s parameter would change to the new parameter passed to the new timer… With the previous struct I have a

question:

Is it possible to make an all-inclusive pointer to this type of object inside a method of a class? Can I templatize a method, and not the whole class? Would it work exactly like a function template?

I have a managing class that holds a vector of these “jobs” and takes care of everything fairly well. I just don’t know how to use a templatized function with the struct, or how to utilize templates on a single method in a class..

I’m also utilizing this in my custom simple threadpool, and that’s working fairly well, and has the same problems…

I have another question:

Can I possibly store a function with a parameter before it’s run? Something like toRun = dontrunmeyet(withThisParameter);? Is my struct even necessary?

Am I going about this whole thing incorrectly?

If this is overly ambiguous, I can set you up with my whole code for context

  • 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-21T20:32:30+00:00Added an answer on May 21, 2026 at 8:32 pm

    In order to create a class method that takes a template parameter, yes, it would work almost exactly like a function template. For example:

    class A
    {
            public:
                    template<typename T>
                    void my_function(const T& value) { }
    };
    
    int main()
    {
            A test;
            test.my_function(5);
    
            return 0;
    }
    

    Secondly, for your structure, you can actually turn that into a functor-object that by overloading operator(), lets you call the structure as-if it were a function rather than having to actually call the specific function pointer members inside the structure. For instance, your structure could be re-written to look like this:

    #include <iostream>
    
    template <class ReturnType, class ParameterType>
    class funcpar
    {
        private:
            ReturnType (*function)(ParameterType);
            ParameterType parameter;
    
        public:
            funcpar(ReturnType (*func)(ParameterType),ParameterType param):
                function(func), parameter(param) {}
    
            funcpar& operator=(const funcpar& fp);
    
            //operator() overloaded to be a function that takes no arguments
            //and returns type ReturnType
            ReturnType operator() ()
            {
                return function(parameter);
            }
    };
    
    
    int sample_func(int value)
    {
        return value + 1;
    }
    
    
    int main()
    {
        funcpar<int, int> test_functor(sample_func, 5);
    
        //you can call any instance of funcpar just like a normal function
        std::cout << test_functor() << std::endl; 
    
        return 0;
    }
    

    BTW, you do need the functor object (or your structure, etc.) in order to bind a dynamic parameter to a function before the function is called in C/C++ … you can’t “store” a parameter with an actual function. Binding a parameter to a function is actually called a closure, and in C/C++, creating a closure requires a structure/class or some type of associated data-structure you can use to bind a function with a specific parameter stored in memory that is used only for a specific instance of that function call.

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

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.