casting

static_cast vs dynamic_cast

自闭症网瘾萝莉.ら 提交于 2019-12-20 12:29:26
问题 Suppose I'm given a C++ library full of inheritance. I'm given a Base* in a function when I know that it is actually pointing to a Derived object and Derived inherits Base . But I don't know what kind of inheritance it is (public/protected/private). I also don't know if there is any virtual function in the hierarchy. Given this situation, without looking into the source code/documentation of Base and Derived , which cast should I use? Or should I consult the code/documentation first to ensure

Cast performance from size_t to double

风流意气都作罢 提交于 2019-12-20 11:07:15
问题 TL;DR : Why is multiplying/casting data in size_t slow and why does this vary per platform? I'm having some performance issues that I don't fully understand. The context is a camera frame grabber where a 128x128 uint16_t image is read and post-processed at a rate of several 100 Hz. In the post-processing I generate a histogram frame->histo which is of uint32_t and has thismaxval = 2^16 elements, basically I tally all intensity values. Using this histogram I calculate the sum and squared sum:

How to cast the size_t to double or int C++

匆匆过客 提交于 2019-12-20 10:29:32
问题 My question is that I have a size_t data, but now I want to convert it to double or int. If I do something like size_t data = 99999999; int convertdata = data; the compiler will report warning. because it maybe overflow. Do you have some method like the boost or some other method to do the convert? 回答1: A cast, as Blaz Bratanic suggested: size_t data = 99999999; int convertdata = static_cast<int>(data); is likely to silence the warning (though in principle a compiler can warn about anything

“warning: use of old-style cast” in g++ [duplicate]

会有一股神秘感。 提交于 2019-12-20 10:18:42
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: When should static_cast, dynamic_cast and reinterpret_cast be used? With this C++ code, char* a = (char*) b; I got warning warning: use of old-style cast . What would be the new-style cast? 回答1: reinterpret_cast , static_cast , dynamic_cast and const_cast are the c++ cast alternatives. const_cast to remove const/volatile from a const variable. dynamic_cast to perform runtime validity checks when casting in

Difference between long and int in C#?

坚强是说给别人听的谎言 提交于 2019-12-20 10:17:37
问题 What is the actual difference between a long and an int in C#? I understand that in C/C++ long would be 64bit on some 64bit platforms(depending on OS of course) but in C# it's all running in the .NET runtime, so is there an actual distinction? Another question: can an int hold a long (by cast) without losing data on all platforms? 回答1: an int (aka System.Int32 within the runtime) is always a signed 32 bit integer on any platform, a long (aka System.Int64 ) is always a signed 64 bit integer on

“Specified cast is not valid” when populating DataTable from OracleDataAdapter.Fill()

被刻印的时光 ゝ 提交于 2019-12-20 10:08:53
问题 I can't seem to find this question anywhere on Google (or StackOverflow), which really surprised me, so I'm putting it on here to help others in the same situation. I have a SQL query which runs fine on Oracle Sql Developer, but when I run it through C# using adapter.Fill(table) to get the results, I get Specified cast is not valid errors ( System.InvalidCastException ). Here is cut-down version of the C# code: var resultsTable = new DataTable(); using (var adapter = new OracleDataAdapter(cmd

Generic method to type casting

最后都变了- 提交于 2019-12-20 10:06:11
问题 I'm trying to write generic method to cast types. I want write something like Cast.To<Type>(variable) instead of (Type) variable . My wrong version of this method: public class Cast { public static T To<T>(object o) { return (T) o; } } And this is simple test: public class A { public static explicit operator B(A a) { return new B(); } } public class B { } A a = new A(); B b = Cast.To<B>(a); As you guessed, this code will fail with InvalidCastException . Is this code fail because virtual

C# 'is' type check on struct - odd .NET 4.0 x86 optimization behavior

房东的猫 提交于 2019-12-20 09:58:02
问题 Update: I have filed a bug report with Microsoft Connect, please vote for it! Update 2: Microsoft have marked the bug report as fixed Posted by Microsoft on 18/08/2010 at 17:25 This bug will be fixed in a future version of the runtime. I'm afraid it's too early to tell if that will be in a service pack or the next major release. Since upgrading to VS2010 I'm getting some very strange behavior with the 'is' keyword. The program below (test.cs) outputs True when compiled in debug mode (for x86)

How to cast away the volatile-ness?

社会主义新天地 提交于 2019-12-20 09:47:58
问题 How to cast away the volatile-ness? Which c++ style cast should I use? 回答1: Use const_cast . For example, volatile sample *pvs = new sample(); sample *ps = const_cast<sample*>(pvs); //casting away the volatile-ness That is, const_cast is used to cast away both const-ness as well as volatile-ness. Unfortunately, its name doesn't contain the term "volatile". Maybe, that is because the keyword const is more common in use than the keyword volatile . As one of the comment says, cv_cast would have

convert from long long to int and the other way back in c++

自古美人都是妖i 提交于 2019-12-20 09:38:07
问题 How to convert from long long to int and the other way back in c++ ?? also what are the properties of long long , especially its maximum size, thank in advance .. 回答1: Type long long is typically 64 bits. Type int is likely to be 32 bits, but not on all machines. If you cast an int to a long long, you can do my_long_long = (long long) my_int and it will be just fine. If you go the other direction, like my_int = (int) my_long_long and the int is smaller than 64-bits, it won't be able to hold