简体   繁体   English

如何重载类成员变量上的赋值运算符

[英]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.我正在尝试跟踪我将在 API 函数中输入的变量的值。 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 .首先,摆脱val Only have the private m_value value.只有私有m_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. operator=应该返回对类的引用( return *this; ),而不是值。

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).您可能需要重载更多运算符,并将对Monitor的引用传递到val (如果内存不足,可以使用一些技巧来计算它)。

You can (in modern C++) even overload the & operator, but unless the API function is a template, it has to return pointer.您甚至可以(在现代 C++ 中)重载&运算符,但除非 API 函数是模板,否则它必须返回指针。 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).在调试期间,您通常可以设置一个内存观察点,该观察点将在写入或什至读取特定内存位置时暂停程序执行(对于 GDB,请参阅 设置观察点;VS 应该具有类似的功能)。 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.如果没有调试器,您可以使用内存保护技巧(例如保护包含变量的页面,并在第一个 SEGV 上取消保护)制作一次性手表,但这对于正常使用来说太脆弱了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重载对类的两个实例变量求和的赋值运算符? - How do overload an assignment operator that sums two instance variable of a class? 如何为类成员重载operator &lt;&lt;? - How to overload operator<< for a class member? 如何重载&lt;&lt;运算符以输出作为类成员的向量 - how to overload << operator to output a vector that is a member of a class 模板类的重载赋值运算符 - overload assignment operator for template class 如何重载从c ++中继承模板的类的赋值运算符 - How to overload the assignment operator for a class that inherits from a template in c++ 是否有必要为具有另一个类 B 的数据成员的类 A 重载赋值运算符和复制构造函数? - Is it necessary to overload the assignment operator and the copy constructor for a class A which has a data member of another class B? 成员函数中类的枚举成员的重载运算符&lt;&lt; - Overload operator << for enumerated member of a class in member functions 包含唯一指针成员变量的类的 C++ 赋值运算符 - C++ Assignment operator for class that contains a unique pointer member variable 如何重载&lt;&lt;操作符以打印类成员? - How do I overload the << operator to print a class member? 如何为类模板定义非成员运算符重载? - How to define non-member operator overload for class template?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM