1

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
  • 2
    Why are you using size_t? Why not use unsigned int or long? Also, what if someone wants a negative fraction? You might want to add a boolean sign unless you're willing to make either the numerator or the denominator (or both) signed. Commented May 26, 2009 at 4:39

5 Answers 5

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

that integral division always catches me
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.
Thank you. I used the double version of your operator float() const.
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.)
3

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... }; 

Comments

2

You can use the operator double to convert:

struct fraction { operator double() const { //remember to check for denominator to 0 return (double)numerator/denominator; } }; 

Comments

1

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; } 

1 Comment

edited to fix syntax (thought I already had but clearly hadn't, sorry)
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.