Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2002
    Location
    middle of nowhere
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Explicit Class Conversions??

    I have a long math class which is simply a pointer to a string(for flexible length) and an unsigned long for the strlen() (so i dont have to call the function every time i need it). I'm in the process of reworking it now, and I isolated all the whole number math and set that up as my base class. It overrides the operators so I can use the class like a standard number type. I implemented them as virtual to try and get a feel for polymorphism. The whole number class is solid, and i started work on the integer class, deriving it from the whole numbers class. There's a bunch of stuff I can reuse, so it looks like the best way to go, but my question is:

    What do I have to override to explicitly convert one number type to the other?

    It would be easy to write, the integer class only introduces one new data member: a boolean sign. I want to be able to cast the integer to a whole number(despite the risk of losing info), and vice-versa.

    So is it another constructor or assignment operator or something? I have a virtual assignment op in the base class, and I was hoping that simply by carefully coding the derived operator(in the integer class) it would be able to AT LEAST cast a whole number to an integer, but it doesnt want to do it.

    Thanks in advance.

  • #2
    New Coder
    Join Date
    Oct 2002
    Location
    middle of nowhere
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just for a further clarification, I know I could just write a function to do this, I just want to be able to say something like:

    Whole a = "100";
    Integer b = a;

    OR

    Integer b(a);// like a copy constructor

    I have no problem writing a conversion, I just want to be able to implement it like you can with regular numbers.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •