The following is not exactly what I'm trying to do but has been simplified to better illustrate the problem.
I've got some code that looks something like this:
Now, let's say I have a function that takes a Double like this:Code:class Double { public: Double (double initialValue) : value(initialValue) {}; virtual ~Double () {}; virtual double get () const { return value; } virtual double set (const double newValue) { return value = newValue; } private: double value; }; // Double class Add : public { public: Add (const Double& lhs, const Double& rhs) : left(lhs), right(rhs) {}; double get () const { return left.get() + right.get(); } private: const Double& left; const Double& right; }; // Add
I want to be able to do all of the following:Code:void printMe (const Double& double) { cout << double.get() << endl; }
The thing is, I want the implicit conversions from double to work, but when I don't use the implicit conversion and instead pass in an already declared Double object, I do not want that object to be copied.Code:Double double1(1.0); Double double2(1.0); printMe(double1 + double2); printMe(double1 + 1.0); printMe(1.0 + double2); printMe(1.0 + 1.0);
I could do this...
...but this code seems to insist on calling the Add copy constructor and produces a warning about returning a reference to a temporary.Code:const Add& operator+ ( const Double& lhs, const Double& rhs ) { return Add(lhs, rhs); }
I could also do this...
...but then I have no way of knowing when the Add object needs to be destroyed.Code:const Add& operator+ ( const Double& lhs, const Double& rhs ) { return *(new Add(lhs, rhs)); }
I need to return a reference, not a pointer, because there is one of these classes for every operator, and I needs compound statements like (x + y) / z to work.
To reiterate: I have a base class - let's call it Double - that cannot be copied. I then have operator classes (like Add) derived from Double that store references to two other Doubles. I need overloaded operator functions to instantiate these operator classes and return them, taking in both already-created Double objects (without copying them) or temporaries implicitly-created at the call.
I cannot figure out how to do this without causing memory leaks, compiler warnings, or copy-constructor calls. Any ideas?
As for why I need to do this: there are other classes that derive from Double (though it's not really called that). These classes do not store an internal value but instead get values from external hardware; such classes are difficult and expensive to copy. Then, I have a function (let's call it control()) that takes in two arguments - a Double called "input" and a Double called "output". This function spawns an OS task with a timer that periodically sends the input to the output. Here's an example function call:
In this case, the expression "input + 1.0" must be reevaluated every time the timer goes off - not just once at the function call. It is sort of like a nullary lambda function.Code:control(input + 1.0, output);
Thanks for any assistance.




Reply With Quote