views:

420

answers:

5

Forgive what might seem to some to be a very simple question, but I have this use case in mind:

struct fraction {
    fraction( size_t num, size_t denom ) : 
        numerator( num ), denominator( denom )
    {};
    size_t numerator;
    size_t denominator;
};

What I would like to do is use statements like:

fraction f(3,5);
...
double v = f;

to have v now hold the value represented by my fraction. How would I do this in C++?

+1  A: 

operator= has nothing to do with it, rather you want to add to your struct a public operator double something like:

operator double() {
  return ((double) numerator))/denominator;
}
Alex Martelli
Alex Martelli
edited to fix syntax (thought I already had but clearly hadn't, sorry)
Alex Martelli
+7  A: 

One way to do this is to define a conversion operator:

struct fraction
{
  size_t numerator;
  size_t denominator;

  operator float() const
  {
     return ((float)numerator)/denominator;
  }
};

Most people will prefer not to define an implicit conversion operator as a matter of style. This is because conversion operators tend to act "behind the scenes" and it can be difficult to tell which conversions are being used.

struct fraction
{
  size_t numerator;
  size_t denominator;

  float as_float() const
  {
     return ((float)numerator)/denominator;
  }
};

In this version, you would call the as_float method to get the same result.

1800 INFORMATION
that integral division always catches me
1800 INFORMATION
It's another of those C/C++ gotchas. +1 for as_float though. This is the same reason there's c_str() on std::string. It's just too easy to get yourself into trouble with implicit conversions. Although for a numeric case like this, it may well be just fine.
Eclipse
Thank you. I used the double version of your operator float() const.
carleeto
Also, use static_cast :)
GMan
I believe you can use "explicit operator float() const" to make the cast only work when explicit, and not be available for behind-the-scenes shenanigans. Then you'd have to say "double myDouble = static_cast<float>( myFraction );" or equivalent. (Actually, just googled this, it looks like it's only standard in C++0x. Most of the compilers I've worked with have supported it for a while.)
leander
A: 

With that much of code it will be compiler error as the compiler doesn't how to convert struct fraction into a double. If you want to provide the conversion then you have to define the operator double() which will be used by the compiler for this conversion.

Naveen
+2  A: 

You can use the operator double to convert:

struct fraction
{
     operator double() const
      {
         //remember to check for  denominator to 0
          return (double)numerator/denominator;
      }
};
aJ
+3  A: 

Assignment operators and conversion constructors are for initializing objects of your class from objects of other classes. You instead need a way to initialize an object of some other type with an object of your class. That's what a conversion operator is for:

struct fraction {
     //other members here...
     operator double() const { return (double)numerator / denominator;}
     //other members here...
};
sharptooth