   Question about type conversion
  Hi,if i have a variable, lets say:int x = something;and i want to store different types of data in this value, can i do something like:float f = 10.02;x = *(int*)&fand thenf = *(float*)&x ??i have tested it, it seemd to work.... but i remember that this is not a very good practice. it is risky? NOTE: my project is very performance critical and dynamic, so classes and templates are no choice.thanksGammastrahler 
  Why not just use a union? ;quote:Original post by SiCrane  Why not just use a union?   Unions don''t help much...are you just trying to typecast something, or are you trying typecast something to a pointer, or are you trying to typecast a pointer to print out a different result...what are you trying to do with that code?  It''s obvious that you are using pointers, but is that your intention?...Dan... ;  @ xdanillumx:I have an array of 6 32 bit integers which i use as registers for my scripting language.and i need to store different types of data in them, depending on arguments and variable types. ;  As SiCrane said, a union is ideal here. union Register{    int intVal;    float floatVal;}Register r;r.intVal = 3;float f = r.floatVal; "Sneftel is correct, if rather vulgar." --Flarelocke ;quote:Original post by Gammastrahler  NOTE: my project is very performance critical and dynamic, so classes and templates are no choice.Classes and templates do not decrease performance, as long as they are used properly. That''s a misconception. Just because something is convenient, doesn''t mean that it''s wrong.   "Sneftel is correct, if rather vulgar." --Flarelocke ;As long as you only want to store values that are the same size as int, then this works well on all desktop PCs/Macs that you''re likely to run your software on, and the consoles, to boot.I still think the union would work better, though. There''s no extra cost to a union, compared to the cast. ;quote:Original post by xdanillumx  Unions don''t help muchThat''s a very broad statement.They can be very useful at times.quote:Original post by Gammastrahler  I have an array of 6 32 bit integers which i use as registers for my scripting language.and i need to store different types of data in them, depending on arguments and variable types.typedef union {   int Integer;   float Float;   void * VoidPtr;...};  I should also point out both classes and templates can be very fast and dynamic.There might be some old 16 bit systems on which your method would fail (due to the size of the floating point being greater in size than the integer) but I wouldn''t worry.Here''s what I''d do:template< unsigned int ByteSize > class Register {  char Data[ByteSize];public:  template < typename T > inline T & Value( void ) const  {    assert( sizeof(T) <= ByteSize );    return *((T *)&Data);  }};Register< 4 > ScriptRegisters[6];ScriptRegister[0].Value< int > = 30;std::cout << ScriptRegister[0].Value< int > << std::endl;  assuming I really really really needed to use register variables like this. Note that assert should get compiled out when not debugging... so that''s not a preformance issue. More readable and compiled just the same, if the compiler has any brains. ;  tbh, anything which relies on casting about like that is a debug nitemare waiting to happen imo ;quote:Original post by Gammastrahler  Hi,if i have a variable, lets say:int x = something;and i want to store different types of data in this value, can i do something like:float f = 10.02;x = *(int*)&fand thenf = *(float*)&x ??i have tested it, it seemd to work.... but i remember that this is not a very good practice. it is risky? Yes. It relies on all sorts of fragile assumptions. Your basic problem is that you need polymorphic behaviour while C++''s default mode of operation is non-polymorphic. You need to define your own type hierarchy, and utilise dynamic despatch to introduce polymorphic behaviour. And yes, that does mean writing your own classes to re-implement the behavioural aspects of int and float you are interested in.