简体   繁体   中英

C++ Overload Typecast to Double

I'm working on an another assignment and I can't figure out how to overload a typecast to double. I need to provide the implementation. Would appreciate some help. Thanks!

Here's the function prototype/declaration in my HugeInteger.h file.

operator double(void)const;

Here's a sample of code to test the overloaded type cast to double operator.

cout << "\n****** Test overloaded type cast to double operator ******\n";
    cout << "\nA = " << A << "\nB = " << B << "\n";
    double dA = (double)A;             // one way to invoke cast operator
    double dB = static_cast<double>(B); // another way to invoke cast operator
    cout << "\nA cast to a double is: " << dA;
    cout << "\nB cast to a double is: " << dB << '\n' << endl;
    struct Money {
         operator double() { return _amount; }

     private:
          double _amount;
   };

   int main() {
       Money Account;
      double CashOnHand = Account;
   }

This is a demo in Micrsoft.It's a typecast(auto) from user's type to double. So I think it's a good way to do your work. You are not limited to the function ,you can do more in it ,or build a frame around it. I hope this is helpful. By the way , ambiguous !!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM