casting

How do C/C++ compilers handle type casting between types with different value ranges?

家住魔仙堡 提交于 2019-12-28 12:32:46
问题 How do type casting happen without loss of data inside the compiler? For example: int i = 10; UINT k = (UINT) k; float fl = 10.123; UINT ufl = (UINT) fl; // data loss here? char *p = "Stackoverflow Rocks"; unsigned char *up = (unsigned char *) p; How does the compiler handle this type of typecasting? A low-level example showing the bits would be highly appreciated. 回答1: Well, first note that a cast is an explicit request to convert a value of one type to a value of another type . A cast will

what is the difference between c-like casting & functional casting? [duplicate]

≡放荡痞女 提交于 2019-12-28 05:53:59
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: C++ cast syntax styles C++: What's the difference between function(myVar) and (function)myVar ? What is the difference between (type)value and type(value) ? b = (int) a; // c-like cast notation b = int (a); // functional notation 回答1: Apparently I was wrong in my initial cut at an answer. They are roughly equivalent. And while compound type names like long long or void * can't use functional syntax directly (i

Conversion from void* to the pointer of the base class

橙三吉。 提交于 2019-12-28 05:46:46
问题 I have some hierarchy: base, derived classes and some structure storing user data as void*. That void can store both Base and Derived classes pointers. Main problem that I do not know what is stored there base or derived pointer. class Base { public: int type; }; class Derived: public Base {}; Base* base;//init base pointer Derived* derived;//init derived pointer void* base_v = base; void* derived_v = derived; //void pointers are correct. They point to base and derived variables. //try to get

Why doesn't this reinterpret_cast compile?

邮差的信 提交于 2019-12-28 04:59:07
问题 I understand that reinterpret_cast is dangerous, I'm just doing this to test it. I have the following code: int x = 0; double y = reinterpret_cast<double>(x); When I try to compile the program, it gives me an error saying invalid cast from type 'float' to type 'double What's going on? I thought reinterpret_cast was the rogue cast that you could use to convert apples to submarines, why won't this simple cast compile? 回答1: By assigning y to the value returned by the cast you're not really

CASTING attributes for Ordering on a Doctrine2 DQL Query

拜拜、爱过 提交于 2019-12-28 03:53:47
问题 I am trying to get Doctrine2 Entities , ordered by their ID which apparently is a String even though it contains only Numbers. So what I would like to do is something like this: SELECT entity1, cast (entity1.id AS integer) AS orderId FROM Namespace\Bla\MyEntity ORDER BY orderId Is there a way to do something like this in Doctrine2 ? Or, what would be the best practise to get my Result if i can't change the type of the id ( due to customer requirements of course )? Attention : I am not asking

Change type of varchar field to integer: “cannot be cast automatically to type integer”

久未见 提交于 2019-12-28 03:17:05
问题 I have a small table and a certain field contains the type " character varying ". I'm trying to change it to " Integer " but it gives an error that casting is not possible. Is there a way around this or should I just create another table and bring the records into it using a query. The field contains only integer values. 回答1: There is no implicit (automatic) cast from text or varchar to integer (i.e. you cannot pass a varchar to a function expecting integer or assign a varchar field to an

C# params object[] strange behavior

拥有回忆 提交于 2019-12-28 03:04:53
问题 Considering this code namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] strings = new string[] { "Test1", "Test2", "Test3" }; int[] ints = new int[] { 1, 2, 3, 4 }; Test(strings); Test(ints); } public static void Test(params object[] objects) { } } } And this page https://msdn.microsoft.com/fr-ca/library/w5zay9db.aspx I would expect (params object[] objects) to be an array of one element with a string[] as the first element, but when I debug, I see

Pass an Objective-C object to a function as a void * pointer

别等时光非礼了梦想. 提交于 2019-12-28 02:52:48
问题 I have a function: myFunction (MyProc callback, void * ref) This function is called from within an Objective-C class. The function is passed a pointer to the callback (a function in the class) and a reference. The reference is necessary because the callback is called statically and therefore doesn't have a context. The ref can be used to provide a context to the callback. I want to be able to pass the Objective-C class as the reference. So the question is: How do I cast an NSObject to a void

PHP unexpected result of float to int type cast

匆匆过客 提交于 2019-12-28 00:56:13
问题 I'trying to convert a float to an int value in php: var_dump((int)(39.3 * 100.0)); //Returns 3929 but should be 3930! var_dump((int)(39.2 * 100.0)); //Returns 3920 I can use ceil to make it work but can somebody explain this to me? var_dump((int)ceil(39.3 * 100.0)); //Returns 3930 回答1: This is because numbers that have a finite representation in base 10 may or may not have an exact representation in the floating point representation PHP uses. See >php -r "echo var_dump(sprintf('%.40F', 39.3 *

PHP unexpected result of float to int type cast

删除回忆录丶 提交于 2019-12-28 00:54:52
问题 I'trying to convert a float to an int value in php: var_dump((int)(39.3 * 100.0)); //Returns 3929 but should be 3930! var_dump((int)(39.2 * 100.0)); //Returns 3920 I can use ceil to make it work but can somebody explain this to me? var_dump((int)ceil(39.3 * 100.0)); //Returns 3930 回答1: This is because numbers that have a finite representation in base 10 may or may not have an exact representation in the floating point representation PHP uses. See >php -r "echo var_dump(sprintf('%.40F', 39.3 *