casting

Multiple inheritance casting from base class to different derived class

六月ゝ 毕业季﹏ 提交于 2019-12-18 04:34:33
问题 Let's assume there is such class hierarchy: class A //base class class B //interface class C : public A, public B Then C object is created: A *object = new C(); Is it possible to cast object to B ? Important: I assume I don't know that object is C. I just know that it implements interface B 回答1: No . This is not possible (direct casting from A* to B* ). Because the address of A and B are at different locations in class C . So the cast will be always unsafe and possibly you might land up in

How to cast variable to array

泪湿孤枕 提交于 2019-12-18 04:28:06
问题 I have a variable $v that can be either single string or array of strings and I have a code: $a = array(); if (is_array($v)) { $a = $v; } else { $a[] = $v; } How it can be done in more elegant way? (in other words, how to cast a variable to array) 回答1: You can cast a variable to an array by using: $var = (array)$arr; 回答2: $a = (array) $v; is the answer. 回答3: I would write your could snippet like this (short and you read it and know exactly what is happening): $a = is_array($v) ? $v : array($v

Is static_cast<T>(…) compile-time or run-time?

北城以北 提交于 2019-12-18 03:58:18
问题 Is static_cast<T>(...) something that gets done at compile-time or run-time? I've googled around but I got different answers. Also, dynamic_cast<T>(...) is obviously runtime - but what about reinterpret_cast<T>(...) ? 回答1: Depends on what you are casting to what else. E.g. static_cast<std::string>("Hello") ends up calling std::string constructor. Off the top of my head, I can't think of any case where reinterpret_cast would need to generate actual machine instructions. It's just telling the

Convert a String list to an Int list

微笑、不失礼 提交于 2019-12-18 03:56:44
问题 I've got a list of strings, is it possible to convert it to an list of ints? E.g.: ["1","2"] -> [1,2] 回答1: Well f :: [String] -> [Int] f = map read No? 回答2: This fails: map read ["1","2"] [*Exception: Prelude.read: no parse The way to do it is : map (read::String->Int) ["1","2"] [1,2] :: [Int] Outside of GHCI, in a .hs file it would be: let intList = map (read::String->Int) ["1","2"] 回答3: The general answer to such questions is to split the task up into parts: [String] -> [Int] looks like a

Why does Arrays.sort take Object[] rather than Comparable[]?

限于喜欢 提交于 2019-12-18 03:56:29
问题 I was wondering why the sort method of the Arrays class is asking for a parameter of type Object[]. Why the parameter is not of type Comparable[]. If you don't pass a Comparable[] it's generating a ClassCastException. Why ... public static void sort(Object[] a) and not public static void sort(Comparable[] a) ? Thanks 回答1: Because the second form would require a reallocation of the array. Even if you know that your array contains only comparables, you cannot just cast it to Comparable[] if the

How to cast or convert an unsigned int to int in C?

╄→尐↘猪︶ㄣ 提交于 2019-12-18 03:55:26
问题 My apologies if the question seems weird. I'm debugging my code and this seems to be the problem, but I'm not sure. Thanks! 回答1: It depends on what you want the behaviour to be. An int cannot hold many of the values that an unsigned int can. You can cast as usual: int signedInt = (int) myUnsigned; but this will cause problems if the unsigned value is past the max int can hold. This means half of the possible unsigned values will result in erroneous behaviour unless you specifically watch out

C++ int float casting

爱⌒轻易说出口 提交于 2019-12-18 03:49:20
问题 Why is m always = 0? The x and y members of someClass are integers. float getSlope(someClass a, someClass b) { float m = (a.y - b.y) / (a.x - b.x); cout << " m = " << m << "\n"; return m; } 回答1: Integer division occurs, then the result, which is an integer , is assigned as a float. If the result is less than 1 then it ends up as 0. You'll want to cast the expressions to floats first before dividing, e.g. float m = (float)(a.y - b.y) / (float)(a.x - b.x); 回答2: You need to use cast. I see the

Question about const_cast in c++

允我心安 提交于 2019-12-18 03:35:10
问题 all: this is quoted from Effective C++ 3rd editiion const_cast is typically used to cast away the constness of objects. It is the only C++-style cast that can do this. My question is can const_cast add constness to a non-const object? Actually i wrote a small programme trying to approve my thought. class ConstTest { public: void test() { printf("calling non-const version test const function \n"); } void test() const{ printf("calling const version test const function \n"); } }; int main(int

Java Convert 4 bytes to int

陌路散爱 提交于 2019-12-18 03:32:16
问题 i was wondering if the solution for this documented here is still the solution or is there any other way getting an int from 4 bytes? thank you. EDIT: im getting the byte[] from sockets .read EDIT: int recvMsgSize = in.read(Data, 0, BufferSize); if recvMsgSize is -1 i know the connection has been dropped. how do i detect this when im using DataInputStream instead of InputStream? thanks. EDIT: apologies for being a yoyo regarding accepting the right answer. but after mihi's updated final

Storing json, jsonb, hstore, xml, enum, ipaddr, etc fails with “column ”x“ is of type json but expression is of type character varying”

我是研究僧i 提交于 2019-12-17 22:39:33
问题 When using PostgreSQL to store data in a field of a string-like validated type, like xml , json , jsonb , xml , ltree , etc, the INSERT or UPDATE fails with an error like: column "the_col" is of type json but expression is of type character varying ... or column "the_col" is of type json but expression is of type text Why? What can I do about it? I'm using JDBC (PgJDBC). This happens via Hibernate, JPA, and all sorts of other abstraction layers. The "standard" advice from the PostgreSQL team