casting

static_cast parent class to child class C++

こ雲淡風輕ζ 提交于 2020-01-17 05:13:13
问题 Output of this program is "Method B". How can an instance of the parent object call the child class's function through a static_cast? To make things more confusing, if I make method() virtual, then this code outputs "Method A". Can anyone explain what is happening here? class A { public: void method() { cout << "Method A" << endl; } }; class B : public A { public: void method() { cout << "Method B" << endl; } }; int main() { A a; B* bptr = static_cast<B*>(&a); bptr->method(); } 来源: https:/

How to cast an object programmatically at runtime?

Deadly 提交于 2020-01-17 03:43:26
问题 Suppose I have a custom control like: MyControl : Control if I have code like this: List<Control> list = new ... list.Add (myControl); RolloutAnimation anim = list[0]; I know I can do it at compile time but I wanna do it at runtime and access MyControl specific functionality. 回答1: Why do you want to do it at runtime? If you have more types of Controls in your List, that have some specific functionality, but different types, perhaps they should implement a common interface: interface

Reading integers in different endianness from binary file in C++

﹥>﹥吖頭↗ 提交于 2020-01-17 02:17:06
问题 I'm reading an ESRI Shapefile, and to my dismay it uses big endian and little endian at different points (see, for instance, the table at page 4, plus the tables from page 5 to 8). So I created two functions in C++, one for each endianness. uint32_t readBig(ifstream& f) { uint32_t num; uint8_t buf[4]; f.read((char*)buf,4); num = buf[3] | buf[2]<<8 | buf[1]<<16 | buf[0]<<24; return num; } uint32_t readLittle(ifstream& f) { uint32_t num; f.read(reinterpret_cast<char *>(&num),4); //f.read((char*

Google Cast 2.6.0 errors when building iOS project

蹲街弑〆低调 提交于 2020-01-16 08:04:09
问题 Any of you guys had errors when building your iOS project after updating to the new Google Cast 2.6.0 framework? I get the following error: Default initialisation of an object of const type ‘const NSInteger’ (aka ‘const int’). at the following lines in class GCKDevice: /** Device capability flag for video out. */ GCK_EXPORT const NSInteger kGCKDeviceCapabilityVideoOut; /** Device capability flag for video in. */ GCK_EXPORT const NSInteger kGCKDeviceCapabilityVideoIn; /** Device capability

Type casting to another with the same properties

醉酒当歌 提交于 2020-01-16 07:18:11
问题 In one section of my application, I use type generated from xsd scheme - I have 2 version of schemas 2008 and 2009 - type I use is DatumType - in every scheme this type contain the same properties - they are exact, except namespaces. Is there any way how to cast DatumType (2008) to DatumType (2009) so I can work in my application only with one type, instead of two? I am working with c# and win forms, thanks! 回答1: No, there is no way to cast one to the other, because these are two unrelated

Unchecked cast warnings in generic method on parameter that must be used in a List

北战南征 提交于 2020-01-16 03:12:30
问题 In the code below, the type parameter D can be either a List<Byte> or a List<List<Byte>> (it is the third generic parameter in the Fields<?, ?, D> interface but still I might omit it there - but it is present also in the return type of the method). Can't seem to find a way to tell the compiler this - get Unchecked cast warnings in the lines marked //* : public static <D, K, T extends Enum<T> & Fields<?, ?, D>> List<EnumMap<T, D>> getEntries(InputStream is, Class<T> fields) throws IOException

Reliable integer typecasting

与世无争的帅哥 提交于 2020-01-16 01:06:13
问题 I'm working on a data validator package for PHP (mostly as an exercise in some of the new additions in 5.3 such as namespaces). I'm planning to have a main validator class which uses plugin classes to do the actual validation. The first validation plugin I'm building is just a simple one for integers. The source is below: namespace validator\validatorplugins; class Integer implements ValidatorPlugin { private $field = NULL; public function cast () { return ($this -> field = (int) ($this ->

System.InvalidCastException while casting an object type to custom class

喜夏-厌秋 提交于 2020-01-15 12:06:48
问题 Today working on my code I came across a phase where i needed to cast an object type to my custom class which should have been very simple, but I ended up hours resolving System.InvalidCastException , finally I ended up using JsonConvert to get the job done. I am not really sure if there was a better way to handle this, if there is I would like to know what I could have done better. Given is the structure of my custom class using SQLite.Net.Attributes; namespace DataContract { public class

mysql cast as interval

匆匆过客 提交于 2020-01-15 11:50:08
问题 is it possible to cast as an interval in MySQL? I want to evaluate a value from the query as an interval, like this: select DATE_SUB(NOW(),CAST(user.expiry_interval AS interval)) where user.expiry_interval is 'INTERVAL 1 WEEK' or something like that select DATE_SUB(NOW(),CAST('INTERVAL 1 week' AS INTERVAL)) 回答1: INTERVAL is not a data type, but a keyword that converts a human-readable "duration" string into an integer. As such, "casting to INTERVAL " makes no sense. If your "interval" input

Flex and Zend_AMF: How do I get a Flex arrayCollection from Flex into PHP?

半世苍凉 提交于 2020-01-15 09:53:09
问题 I currently have an arrayCollection in Flex and I want to sent it to PHP (Zend_AMF). According to the Zend_AMF wiki, sending an arrayCollection over directly will force Zend_AMF to cast the arrayCollection as an object which is no good. I'd rather have an array of my models. I assume the best way would be to convert the arrayCollection to an array in flex and then send it over. Is this true, and if so how would I do that in Flex 3? If you have a better recommendation, that would be