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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:14:55+00:00 2026-06-12T20:14:55+00:00

I would love create javascript’s setTimeout and setInterval like functionality for c++ (without boost).

  • 0

I would love create javascript’s setTimeout and setInterval like functionality for c++ (without boost). What I would like to achieve: A base class which could call its sub-classed member variables at a repeated or after a single delay.

I have an update function already and time elapsed functionality. I have also found how to pass a member function pointer to the base class and trigger that function using:

class BaseClass {
public:
     template <class object>
     void triggerNow(object *obj, void (object::*func)()) {
          ((obj)->*(func))();
     }

} 

class SubClass : public BaseClass {
public:
    void update() {
         triggerNow(this, &SubClass::worked)
    }
    void worked() {
         cout << "worked!";
    }
}

The problem I currently face is how to store object *obj and void (object::*func)() in a vector (or other container). I am only just figuring out templates…

How can I store the two templated parameters of triggerNow in a vector? Once I can figure this out, I can create my setTimeout and setInterval!

  • 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-12T20:14:56+00:00Added an answer on June 12, 2026 at 8:14 pm

    In order to store object* and object::*func in a vector you can do it like this:

    struct Callable
    {
      virtual ~Callable () {};
      virtual void operator()() = 0;
    };
    
    template <class Object>
    struct TemplateCallable : public Callable
    {
      typedef void (Object::*MemberFunction)();
    
      TemplateCallable(Object* obj, MemberFunction mem_fun)
         : _object(obj),
           _mem_fun(mem_fun)
      {}
    
      void operator() ()
      {
        _object->*_mem_fun();
      }
    
      Object* _obj;
      MemberFunction _mem_fun;
    };
    

    Now you can store them as Callable’s in a std::vector, because the templated version is derived from Callable. If you store pointers in a std::vector<> though you need to remember to delete them at some point.

    So will get something like this:

    class TimerSomething
    {
       template <class Object>
       void registerCallback (Object* obj, void (Object::*MemFun)())
       {
         _callbacks.push_back(new TemplatedCallback<Object>(obj, mem_fun));
       }
    
       void triggerAll ()
       {
         for (std::vector<Callable*>::iterator iter = _callbacks.begin();
              iter != _callbacks.end(); ++iter)
         {
           (**iter)();
         }
       }
    
       std::vector<Callable*> _callbacks;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to create a interval which every hour import data from sql
I would like to create a game in wireframe mode however without the glPolygoneMode
I'm pretty new to wordpress, html, css, and javascript and would love your help
I would like to create a pause inside a while loop so that I
I would like to create a python function that would allow me to iterate
I have design problem, which I would love to get a hand with. I
I would like to create an Access database from a script. Ideally, I would
I would like to simplify this code and would love any suggestions. Structure of
Would love some opinions on this problem I'm trying to workout. I'm trying to
I would love to get some tips from other people that have had this

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.