<p>Is it possible to create custom events in C++?  For example, say I have the variable X, and the variable Y.  Whenever X changes, I would like to execute a function that sets Y equal to 3X.  Is there a way to create such a trigger/event?  (triggers are common in some databases)</p>

[{'answer_id': 78013, 'author': 'epatel', 'author_id': 842, 'author_profile': 'https://Stackoverflow.com/users/842', 'pm_score': 1, 'selected': False, 'text': '<p>Think you should read a little about <a href="http://en.wikipedia.org/wiki/Design_pattern_(computer_science)" rel="nofollow noreferrer">Design Patterns</a>, specifically the <a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer">Observer Pattern</a>.</p>\n\n<p>Qt from <a href="http://trolltech.com/" rel="nofollow noreferrer">Trolltech</a> have implemented a nice solutions they call <a href="http://www.youtube.com/watch?v=yPhbgrApNx0" rel="nofollow noreferrer">Signals and Slots</a>.</p>\n'}, {'answer_id': 78016, 'author': 'slf', 'author_id': 13263, 'author_profile': 'https://Stackoverflow.com/users/13263', 'pm_score': 1, 'selected': False, 'text': '<p>Use the Observer pattern</p>\n\n<p><a href="http://www.codeproject.com/KB/cpp/CppEvents.aspx" rel="nofollow noreferrer">code project example</a></p>\n\n<p><a href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer">wiki page</a></p>\n'}, {'answer_id': 78025, 'author': 'Denice', 'author_id': 14178, 'author_profile': 'https://Stackoverflow.com/users/14178', 'pm_score': 1, 'selected': False, 'text': "<p>As far as I am aware you can't do it with default variables, however if you wrote a class that took a callback function you could let other classes register that they want to be notified of any changes.</p>\n"}, {'answer_id': 78135, 'author': 'Adam Wright', 'author_id': 1200, 'author_profile': 'https://Stackoverflow.com/users/1200', 'pm_score': 3, 'selected': False, 'text': '<p>This is basically an instance of the Observer pattern (as others have mentioned and linked). However, you can use template magic to render it a little more syntactically palettable. Consider something like...</p>\n\n<pre><code>template &lt;typename T&gt;\nclass Observable\n{\n  T underlying;\n\npublic:\n  Observable&lt;T&gt;&amp; operator=(const T &amp;rhs) {\n   underlying = rhs;\n   fireObservers();\n\n   return *this;\n  }\n  operator T() { return underlying; }\n\n  void addObserver(ObsType obs) { ... }\n  void fireObservers() { /* Pass every event handler a const &amp; to this instance /* }\n};\n</code></pre>\n\n<p>Then you can write...</p>\n\n<pre><code>Observable&lt;int&gt; x;\nx.registerObserver(...);\n\nx = 5;\nint y = x;\n</code></pre>\n\n<p>What method you use to write your observer callback functions are entirely up to you; I suggest <a href="http://www.boost.org" rel="noreferrer">http://www.boost.org</a>\'s function or functional modules (you can also use simple functors). I also caution you to be careful about this type of operator overloading. Whilst it can make certain coding styles clearer, reckless use an render something like</p>\n\n<p>seemsLikeAnIntToMe = 10;</p>\n\n<p>a <em>very</em> expensive operation, that might well explode, and cause debugging nightmares for years to come.</p>\n'}, {'answer_id': 78863, 'author': 'Doug T.', 'author_id': 8123, 'author_profile': 'https://Stackoverflow.com/users/8123', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://www.boost.org/doc/libs/1_36_0/doc/html/signals.html" rel="nofollow noreferrer">Boost signals</a> is another commonly used library you might come across to do Observer Pattern (aka Publish-Subscribe). Buyer beware here, I\'ve heard its performance is terrible.</p>\n'}]
['https://Stackoverflow.com/questions/77996', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/13790/']