Thread: Is it possible to assign a type to a variable?

  1. #16
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Are all the types you convert to integer types (It looks like it, given your "ideal" solution) ? If so, then the solution is trivial, and you won't be needing all this C/C++ boilerplate
    Last edited by Ronix; 06-10-2010 at 11:51 AM.
    Stick close to your desks and never program a thing,
    And you all may sit in the standards commitee!

  2. #17
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Sebastiani View Post
    Make sense?
    ::ROTF:: Uh, no. I didn't understand most of what you wrote, but it looks even more complicated that my C code that just does a switch statement based upon the enumerated types.

  3. #18
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by Ronix View Post
    Are all the types you convert to integer types (It looks like it, given your "ideal" solution) ? If so, then the solution is trivial, and you won't be needing all this C/C++ boilerplate
    Ah to dream.

    The elements in the structures can be any of the following:

    uint8_t
    uint8_t * (Only one in all structures -- it is a user defined description)
    uint16_t
    uint32_t
    float
    double

    So, along the lines your thinking, I could have just assigned an int to the data, then allow the type conversion to occur within the compiler, but nope, it ain't that simple. I have too many cases of floating point numbers to make this work right (that'd be: it is still the same amount of code either way).

  4. #19
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Kennedy View Post
    ::ROTF:: Uh, no. I didn't understand most of what you wrote, but it looks even more complicated that my C code that just does a switch statement based upon the enumerated types.
    Maybe. Even so, given the option of a simple design that's complicated to use, and a complex design that's easy to use, I'd choose the latter any day, personally.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #20
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sebastiani View Post
    Maybe. Even so, given the option of a simple design that's complicated to use, and a complex design that's easy to use, I'd choose the latter any day, personally.
    Altho it's hard to see how a design that you do not understand could be considered "simple to use". I totally followed Kennedy's original idea and don't see why he'd think there is some better option (altho it never hurts to ask).

    Also, your maxim here totally ignores the question of expense. Switching from C to C++ just for this kind of screams "spend" to me.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #21
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Altho it's hard to see how a design that you do not understand could be considered "simple to use".
    Well, the interface is about as simple as it gets: pass a reference to the constructor, and then invoke the object like a function. Easy. If you're talking about the internals of the class, well, consider the STL - how many people *really* understand how it works? Not many!

    Also, your maxim here totally ignores the question of expense. Switching from C to C++ just for this kind of screams "spend" to me.
    Fortunately, in this day and age, the cost is (typically) pretty cheap.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 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. Can you check what is wrong with this code
    By Ron in forum C++ Programming
    Replies: 4
    Last Post: 08-01-2008, 10:59 PM
  4. Replies: 6
    Last Post: 07-29-2008, 04:37 AM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM