Jump to content

  • Log In with Google      Sign In   
  • Create Account


function calling out to others? (c++)


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 suliman   Members   -  Reputation: 558

Like
2Likes
Like

Posted 02 March 2013 - 02:23 AM

Hi

Let me explain, i wanna do a generic function that i can use in many projects, specifically:

 

I have a generic updateDate function that steps through the days, changing months and years etc.

 

I want it to "broadcast" every time month or year changes (or whatever) so i can place triggers anywhere in my code like:

 

 


if(monthJustChanged)
 doStuff();

if(yearJustChanged)
 doMoreStuff();
 

 

thanks!

Erik



Sponsor:

#2 Tribad   Members   -  Reputation: 841

Like
0Likes
Like

Posted 02 March 2013 - 02:33 AM

Create a base class with the doStuff() and doMoreStuff() methods defined as virtual.

Whereever you think to need these methods mix-in this baseclass and define a specific method doStuff() and doMoreStuff().

 

But anyways you need someone who is going through all that doStuff thing.



#3 Nercury   Crossbones+   -  Reputation: 766

Like
1Likes
Like

Posted 02 March 2013 - 06:13 AM

You can do it Java way.
Create DateDispatcher with methods:

  • changeDate(newDate)
  • addListener(listener)

Make another base class for date change listener, BaseDateChangeListener, with virtual methods:

  • virtual onMonthChange(newDate)
  • virtual onYearChange(newDate)

Make DateDisplatcher contain list of listeners and inform them about month and year change when needed.

You can use this DateDispatcher in many projects, it does not depend on specific project stuff.

Then you can make project specific class that "does stuff", named ClassThatDoesStuff that derives from BaseDateChangeListener.

 

class ClassThatDoesStuff : public BaseDateChangeListener {
public:
    virtual void onMonthChange(date newDate) {
       // do project-specific stuff
    }
    virtual void onYearChange(date newDate) {
       // do project-specific stuff
    }
}

 

And add it as listener in DateDispatcher. This way you can keep your project logic separated from this small tool logic.

Edit: for date and time manipulation you can use boost::date_time.


Edited by Nercury, 02 March 2013 - 06:17 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS