Thank you all for your answers. And thanks for the cppinsight and godbolt, did not know these.
I edited my question here what I want to do. Basically I have a SDK acquisition function that has an pointer float argument as input, and I want to monitor that argument in real time to know the status of the acquisition. Now i got this code to work:
#include <iostream>
template <class T>
class MonitoredVariable
{
public:
MonitoredVariable() {}
// MonitoredVariable(const T& value) : m_value(value) {}
operator T&() { return m_value; }
//operator const T&() const { return m_value; }
MonitoredVariable& operator = (float value)
{
printf("Variable modified\n");
this->m_value = &value;
printf("%f \n", *this->m_value);
return *this;
}
MonitoredVariable& operator * () {return *this;}
private:
T m_value;
};
void SDKfunctionIcannotChange(float *a)
{
*a = (float) 5.2;
printf("%f \n", *a); // if I comment this out, x will not update?!?!
return;
}
int main()
{
MonitoredVariable<float*> x;
*x=0;
SDKfunctionIcannotChange(x);
printf("%f \n", *x);
return 1;
}
But strangely if I comment out that printf command in the SDKfunctionIcannotChange function, then the x value will not change. Any ideas why is this happening?
So with printf (correct):
Variable modified
0.000000
5.200000
5.200000
and without
Variable modified
0.000000
0.000000
thanks..