1

I have a class MyFloat:

class MyFloat
{
public:
float value = 0.0;
}

I use it so far in this way:

MyFloat *myfloat1 = new MyFloat;
myfloat1->value = 1.0;

How can I make

myfloat1 = 2.0;
if (myfloat1 < 3.0){
...
}
Marat Gareev
  • 387
  • 1
  • 4
  • 16

3 Answers3

3

You can use operator= and operator float

class MyFloat
{
public:
    float value = 0.0;
    MyFloat& operator=(float f)
    {
        value = f;
        return *this;
    }

    operator float() const
    {
        return value;
    }
};

int main() {

    MyFloat f;
    f = 10.0f;

    float x;
    x = f;
    return 0;
}
R2RT
  • 2,061
  • 15
  • 25
2

As others have mentiond you could provide a operator=(float), however for a MyFloat it is more natural to be constructed with a float instead of first constructing it and then assigning a value:

class MyFloat {
    public:
        float value = 0.0;
        MyFloat(float x) : value(x) {}
        MyFloat() {}
 };

int main() {
    MyFloat x;
    x = 1.0;        // works because MyFloat(float) is used
    MyFloat y(2.0); // this is nicer
}

PS: as pointed out by R2RT, for x = 1.0; you dont want to use the constructor, but an assignment operator (I just left the line because thats what you were asking for). However, for a newly constructed MyFloat I would always prefer to pass the value to the constructor.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • As for me - it should be not 'instead' but 'also' - you create here new instance of MyFloat when assigning - might be constly and unexpected behaviour. – R2RT Aug 11 '17 at 10:40
  • @R2RT I only put the `x=1.0;` because thats what OP was asking for. I agree that for `x=1.0;` using `operator=` is better, but if possible I would always prefer to use the constructor – 463035818_is_not_an_ai Aug 11 '17 at 11:19
  • @tobi303 thanks for your answer! I should point out that I will use the object of the class in comparisons (now I have edited the question). So I need **operator float()** yet. – Marat Gareev Aug 11 '17 at 11:33
  • @MaratGareev I would be careful with allowing implicit casts. Sometimes you dont want your `MyFloat` as a `float`. Anyhow if you treat it as float everywhere I would ask myself if you really need `MyFloat` and not simply a `float` – 463035818_is_not_an_ai Aug 11 '17 at 11:37
  • @MaratGareev btw to allow `myfloat < 3.0` you could also provide an `operator<(float)` – 463035818_is_not_an_ai Aug 11 '17 at 11:37
  • @tobi303 I understand, but then I will have to overload other operators (>, <, >=, <=). I need this class to represent pressure units. This class has methods for converting from the current unit of measure to another, representing as a string with or without a unit name, etc. For ex.: `pressureSensor[3].convertTo(puMillibar).toStringWithUnit()` – Marat Gareev Aug 11 '17 at 11:42
1

Your object is of type MyFloat, not float so you can't assign a float value to it. What you can do is assign a float value to a data member of your class by overriding the class' = assignment operator:

 MyFloat& operator=(float p) {
     value = p;
     return *this;
 }
Ron
  • 14,674
  • 4
  • 34
  • 47