简体   繁体   中英

how to overload assignment operator on class member variable

I am trying to track the value of a variable that I will input in an API function. One option is to overload the assignment operator and put some code there. But how would I overload an assigment operator on a member variable of a class?

#include <iostream>
using namespace std;

template <class T>
class MonitoredVariable1
{

public:
    MonitoredVariable1() {  }
    MonitoredVariable1(const T& value) : m_value(value) {}

    operator T() const { return m_value; }

    T val;

    T& operator = (const T& value)
    {
        val = value;
        m_value = value;
        std::cout << "value updated" << " \n";  //THIS NEVER GET PRINTED!!!

        return val;
    }

private:
    T m_value;
};


int main()
{

    MonitoredVariable1<double> MonitoredVariable;
    MonitoredVariable.val = 10.2;

    std::cout << "main done..."  << " \n";
    return 0;
}

To monitor changes to the variable, you need to be assigning to the class, not the contained variable.

First, get rid of val . Only have the private m_value value. This way all accesses have to go thru your member functions that can track the changes.

operator= should return a reference to the class ( return *this; ), not the value.

Assignment is to the class object:

MonitoredVariable = 10.2;

You can only overload assignment on a class. But you can make that variable to be of a class type with overloaded assignment, like:

class Monitor {
    class Monitored {
        double x;
    public:
        Monitored &operator= (double v) {
            std::cout << "Assigned " << v << std::endl;
            x = v;
            return *this; // don’t forget this!
        }

        operator double() const {
            std::cout << "Accessed " << x << std::endl;
            return x;
        }
    };
    Monitored val;
};

You may need to overload more operators, and also to pass a reference to Monitor into val (there are tricks to calculate it instead if you're short on memory).

You can (in modern C++) even overload the & operator, but unless the API function is a template, it has to return pointer. Watching for access through it is very environment-specific.

During debugging, you can usually set a memory watchpoint that will pause program execution on writing to, or even on reading from, a particular memory location (for GDB, see Setting Watchpoints ; VS should have a similar feature). That requires hardware support (or debugger-interpreter which is insanely slow), though, so the overall number of watchpoints is often very limited.

Without a debugger, you may be able to make a one-shot watch using memory protection tricks (like protecting the page containing the variable, and unprotecting it on first SEGV) but that's all too fragile for normal use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM