M
Michael
Hi,
As an exercise, I created a class C that duplicates a value x
thus having x_val1 and x_val2
I also overrided the + operator so that
if I have z = x + y (x,y and z belong class C) then
+ performs z_val1 = x_val1 + y_val1
z_val2 = x_val2 + y_val2
This works fine but it doesn't allow more than one addition per
equation
ex z = x + y + t (x,y,z and t belong to C)
Can someone please enlighten me how can I do it ?
This is the code I wrote
///////////////////////////////////////////////////////////////////////////////
class Cfloat {
//Creates 2 replicas of a variable.
private:
float _r1; //first replica
float _r2; //second replica
public:
float r1() { return _r1; }
float r2() { return _r2; }
Cfloat(const float x) : _r1(x), _r2(x) {}
friend Cfloat operator +(const Cfloat &,const Cfloat &);
};
Cfloat operator +(Cfloat &op1,Cfloat &op2)
{
float CorrectValue;
float r1 = op1.r1() + op2.r1(),
r2 = op1.r2() + op2.r2();
... {pick one}
return(Cfloat(PickedValue));
}
int main
{
....
Cfloat result;
result = a + b;
result = a + b + c; // If there is more than one operator then the
program errors
}
///////////////////////////////////////////////////////////////////////////////
TIA
Michal
As an exercise, I created a class C that duplicates a value x
thus having x_val1 and x_val2
I also overrided the + operator so that
if I have z = x + y (x,y and z belong class C) then
+ performs z_val1 = x_val1 + y_val1
z_val2 = x_val2 + y_val2
This works fine but it doesn't allow more than one addition per
equation
ex z = x + y + t (x,y,z and t belong to C)
Can someone please enlighten me how can I do it ?
This is the code I wrote
///////////////////////////////////////////////////////////////////////////////
class Cfloat {
//Creates 2 replicas of a variable.
private:
float _r1; //first replica
float _r2; //second replica
public:
float r1() { return _r1; }
float r2() { return _r2; }
Cfloat(const float x) : _r1(x), _r2(x) {}
friend Cfloat operator +(const Cfloat &,const Cfloat &);
};
Cfloat operator +(Cfloat &op1,Cfloat &op2)
{
float CorrectValue;
float r1 = op1.r1() + op2.r1(),
r2 = op1.r2() + op2.r2();
... {pick one}
return(Cfloat(PickedValue));
}
int main
{
....
Cfloat result;
result = a + b;
result = a + b + c; // If there is more than one operator then the
program errors
}
///////////////////////////////////////////////////////////////////////////////
TIA
Michal