Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

4
  • that integral division always catches me Commented May 26, 2009 at 4:46
  • 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. Commented May 26, 2009 at 4:50
  • Thank you. I used the double version of your operator float() const. Commented May 26, 2009 at 4:51
  • 2
    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.) Commented May 26, 2009 at 22:13