[英]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.