Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Tracking primitive type value change
    primarykey
    data
    text
    <p>i have a complex program with weird bug that some int value is down to zero unexpectedly.</p> <p>so i want tracking this built-in type value, then i could debug easily.</p> <p>to do that, i made following ValueWatcher template class so i could track almost changes of value except when ValueWatcher is dereferencing. (i made these dereferencing operators because the program needs int *, &amp;)</p> <pre><code>template &lt;typename T&gt; class ValueWatcher { public: ValueWatcher(const T &amp;val) { cout &lt;&lt; "constructor with raw value " &lt;&lt; val &lt;&lt; endl; _cur = _old = val; } ValueWatcher(const ValueWatcher&amp; vw) { cout &lt;&lt; "constructor with ValueWatcher " &lt;&lt; vw._cur &lt;&lt; endl; _cur = vw._cur; } ValueWatcher&amp; operator=(const ValueWatcher &amp;rhs) { cout &lt;&lt; "operator= with ValueWatcher " &lt;&lt; rhs._cur &lt;&lt; endl; _cur = rhs._cur; onChanged(); return *this; } ValueWatcher&amp; operator=(const T &amp;val) { cout &lt;&lt; "operator= with " &lt;&lt; val &lt;&lt; endl; _cur = val; onChanged(); return *this; } int *operator&amp;() { cout &lt;&lt; "addressing operator" &lt;&lt; endl; // can't track anymore!!!!!!!!!!!!!!!!!!!!!!!!! return &amp;_cur; } operator int&amp;() { cout &lt;&lt; "operator int&amp;" &lt;&lt; endl; // can't track anymore!!!!!!!!!!!!!!!!!!!!!!!!! return _cur; } operator int&amp;() const { cout &lt;&lt; "const operator int&amp;" &lt;&lt; endl; return _cur; } operator int() const { cout &lt;&lt; "operator int" &lt;&lt; endl; return _cur; } private: void onChanged() { // update old and do proper action } T _cur; T _old; }; </code></pre> <p>the problem is, when client code wants int &amp; or int * of ValueWatcher, - it can gives int &amp; or int * anyway but - int * or &amp; cannot hold ValueWatcher instance, so can't tracking anymore.</p> <p>is there anyway to solve this? i think it can be solved by returning reference or pointer class instance instead of just returning &amp; or * of built-int type. but i don't know how to do that.</p> <p>in addition- i can't run this program with debugger. the problem occurs only in REAL environment and very hard to reproduce.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload