CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jun 2010
    Posts
    17

    Question Update all instances of a class with a single funtion?

    First of all, let me say that while I do know about classes and such, stuff like inheritance, templates, and other complex topics is pretty much beyond my level.

    My Timer class structure looks like so:
    Code:
    class Timer
    {
    public:
       double time;
       void Update(void);
    };
    Update() increases time by however much time has passed since the last time Update() was called, normally every frame. time is public, so you can easilly set it to 0 to reset it, or grab it's current value directly, or even set it to a specific value if you need.

    What I want is to call Update() only once, instead of once for every single instance of the class. This not only would clean up my code significantly, but it would speed it up too (less calls to QueryPerformanceCounter(), etc.)

    Any advice?
    Last edited by candlemaster; September 3rd, 2011 at 01:09 AM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by candlemaster
    Update() increases time by however much time has passed since the last time Update() was called, normally every frame. time is public, so you can easilly set it to 0 to reset it, or grab it's current value directly, or even set it to a specific value if you need.

    What I want is to call Update() only once, instead of once for every single instance of the class. This not only would clean up my code significantly, but it would speed it up too (less calls to QueryPerformanceCounter(), etc.)
    Is that all that Update does? It might help if you gave us more information on what this Timer class is for, how it will be used, etc.

    At the moment, it sounds like you are asking for the impossible, at least if you can only process sequentially: all the Timer objects are separate, but somehow they must be updated simultaneously, in the same instant of time.

    If you do want them to be updated like that, it seems to me they must all share a singleton time member. Perhaps they can keep a time member that is added to this singleton time member to produce the resulting time, but whether this is feasible depends on what you are trying to do.

    On the other hand, if you will be satisfied with just calling a function once and having all the Timer objects updated (in sequence), then a possible solution is to have a registry of Timer objects. Upon construction, each Timer object will register itself with this registry. You can then just call a function to update each Timer object.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2010
    Posts
    17

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by laserlight View Post
    On the other hand, if you will be satisfied with just calling a function once and having all the Timer objects updated (in sequence), then a possible solution is to have a registry of Timer objects. Upon construction, each Timer object will register itself with this registry. You can then just call a function to update each Timer object.
    That pretty much describes what I want to do, yeah. How would I go about making a registry?

  4. #4
    Join Date
    Apr 2008
    Posts
    725

    Re: Update all instances of a class with a single funtion?

    Code:
    #include <vector>
    
    class TheRegister
    {
    public:
      void register(Timer* timer)
      {
        m_timers.push_back(timer);
      }
    
      void update()
      {
        //loop for all Timer* in m_timers and call update
      }
    
    private:
      std::vector<Timer*> m_timers;
    };
    keep the register somewhere 'central' or make it into a singleton.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by Amleto View Post
    keep the register somewhere 'central' or make it into a singleton.
    I think I'd probably make the vector of timer pointers a static member of the timer class itself, most likely a private one. The "update all timers" function would then be a (public) static member of that class as well. And timer objects would register themselves in their constructor. That way the whole thing would be pretty self-contained...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Apr 2008
    Posts
    725

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by Eri523 View Post
    I think I'd probably make the vector of timer pointers a static member of the timer class itself, most likely a private one. The "update all timers" function would then be a (public) static member of that class as well. And timer objects would register themselves in their constructor. That way the whole thing would be pretty self-contained...
    in effect, that's a singleton then ...

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by Amleto View Post
    in effect, that's a singleton then ...
    Not quite... A singleton would have a single instance pointer instead of a vector of instance pointers and the instance it points to is reused over and over. Also, the timer objects here would probably get constructed using a public constructor (and register themselves in that process) instead of the instance accessor function that's common to singleton implementations. And from the client code perspective the timer objects would look much more like usual instances rather than a singleton.

    Actually, the design I proposed for the timer class happens to have some similarity with what I like to call a "multi-singleton" which probably is better described as a hybrid of the Singleton pattern and the Named Constructor idiom. It's one of my favorite toys and I'm currently using it in one of my real-life apps and something quite similar in a library module used by many of my apps. In a construct like that, there's a registry of existing objects that get reused when the matching parameters are passed to the named constructor.
    Last edited by Eri523; September 6th, 2011 at 06:46 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Update all instances of a class with a single funtion?

    actually, ( unless I'm missing something ) there's no need of linearly updating each timer from a centralized timer; you can write something like

    Code:
    class Timer
    {
    public:
    	Timer(): time_() {}
       	void Update() { time_ += 0.1; }
    
    	double GetTime() const { return time_; }
    
    private:
       double time_;
    };
    
    class FreeTimer
    {
    public:
    	FreeTimer( Timer& timer ): timer_(&timer), last_abs_time_( timer.GetTime() ), local_time_( timer.GetTime() ) {}
    
    	double PopTime()
    	{
    		local_time_ += timer_->GetTime() - last_abs_time_;
    		last_abs_time_ = timer_->GetTime();
    
    		return local_time_;
    	}
    
    	void PushTime( double time )
    	{
    		local_time_ = 0.;
    		last_abs_time_ = timer_->GetTime();
    	}
    
    private:
    	double 	last_abs_time_;
    	double 	local_time_;
    	Timer*	timer_; // or a smart pointer in real code ...
    };
    
    int main()
    {
    	Timer 		timer;
    	FreeTimer	free_timer1( timer );
    	FreeTimer	free_timer2( timer );
    	
    	while(-1)
    	{
    		timer.Update();
    		
    		std::cout << "absolute timer: " << timer.GetTime() << std::endl;
    		if( /*whatever*/ ) std::cout << "timer 1: " << free_timer1.PopTime() << std::endl;
    		if( /*whatever*/ ) std::cout << "timer 2: " << free_timer2.PopTime() << std::endl;
    		if( /*whatever*/ ) free_timer1.PushTime( 0. );
    		if( /*whatever*/ ) free_timer2.PushTime( 0. );
    	}
    }
    of course, the code above will accumulate numerical errors on free timer values ... but I suppose the OP can live with that given his requirements ...

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by superbonzo
    actually, ( unless I'm missing something ) there's no need of linearly updating each timer from a centralized timer; you can write something like
    Dunno, I mentioned a similiar option in my post #2, but for some reason candlemaster decided that the registry idea would be more appropriate.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by laserlight View Post
    Dunno, I mentioned a similiar option in my post #2,[...]
    so, I did miss something then ...

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Update all instances of a class with a single funtion?

    Sounds like this could be a job for the Observer Pattern
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Apr 2008
    Posts
    725

    Re: Update all instances of a class with a single funtion?

    I said, in effect, which is true. You have one possible accessor, and one possible update method.

    Quote Originally Posted by Eri523 View Post
    Not quite... A singleton would have a single instance pointer instead of a vector of instance pointers and the instance it points to is reused over and over.
    That doesn't work if the timers are meant to be used independently. Since the op wanted multiple timers, it doesnt make sense to me to 'reuse' one.

    Quote Originally Posted by Eri523 View Post
    Also, the timer objects here would probably get constructed using a public constructor (and register themselves in that process) instead of the instance accessor funtion that's common to singleton implementations. And from the client code perspective the timer objects would look much more like usual instances rather than a singleton.
    sure, it might look a bit different syntactically, but in effect they are the same.

  13. #13
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Update all instances of a class with a single funtion?

    No an observer is much different than a singleton.
    ahoodin
    To keep the plot moving, that's why.

  14. #14
    Join Date
    Apr 2008
    Posts
    725

    Re: Update all instances of a class with a single funtion?

    Quote Originally Posted by ahoodin View Post
    No an observer is much different than a singleton.


    nobody said otherwise.

  15. #15
    Join Date
    Mar 2001
    Posts
    2,529

    Re: Update all instances of a class with a single funtion?

    Aaah well here is a good reference on these things:
    http://www.codeguru.com/forum/showthread.php?t=327982
    HTH,
    ahoodin
    To keep the plot moving, that's why.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured