Thread: Object turns itself into another type?

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Object turns itself into another type?

    Hello!

    I have a contairer class Number which is going to be able to contain a few different number types, including an integer class and a fraction class. The thing is that, if a fraction suddenly turns out to have an integer value, it is going to convert itself from a private function, to an integer object. What is the simplest method to achieve that? The number class uses a boost::variant to store the actual number.
    Come on, you can do it! b( ~_')

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why not have a variable describing the "representation", say an enum with the values integer, fraction, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Why not use boost::variant? So, what I really need to know, how can the data type turn the value of the container (which would be itself) into another number type?
    Come on, you can do it! b( ~_')

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I would recommend not transforming it into an integer, but instead Having the fraction class print like an integer when it simplifies to an integer. If you have two classes that change into each other so easily then they should probably just be one class. After all, the interface to fractions and integers should be the same, otherwise you may get surprises when you apply a function to two values and expect a fraction result, but get an integer instead.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no need to morph between integers and fractions (more precisely, "rational numbers": values that can be represented as a ratio of two integers). An integer is simply a fraction that has denominator equal to one.

    If you must, simply support operations that allow conversion between integer and fraction (converting a integer to a fraction will produce a fraction with denominator equal to one, converting a fraction to an integer will convert with rounding as required).

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by King Mir View Post
    I would recommend not transforming it into an integer, but instead Having the fraction class print like an integer when it simplifies to an integer. If you have two classes that change into each other so easily then they should probably just be one class. After all, the interface to fractions and integers should be the same, otherwise you may get surprises when you apply a function to two values and expect a fraction result, but get an integer instead.
    Yeah exactly. Like:
    Code:
    fraction foo(1, 2) // one-half
    fraction bar = (foo+foo) / 3;
    // Now bar = 0, not 1/3
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a map with a new object type
    By blacknail in forum C++ Programming
    Replies: 6
    Last Post: 11-24-2008, 10:16 AM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  3. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM