casting

Object's type in a has-a relationship - C++

无人久伴 提交于 2019-12-24 04:59:15
问题 Consider the following code (also available at C++ Shell). Basically, we have an Employee base class with FullTime and PartTime subclasses. The organization class HAS a list of Employees (who could be Fulltime or PartTime). The problem arises when I want to define a getEmployee(int) method in the Organization class. What type of Employee should it return? If it returns a Employee*: first of all, is it safe to return a pointer? I assume that its address is permanent, because it will send the

Struct pointer casts

天涯浪子 提交于 2019-12-24 04:32:12
问题 I'm trying to implement a linked list like this: typedef struct SLnode { void* item; void* next; } SLnode; typedef struct DLnode { void* item; void* next; struct DLnode* prev; } DLnode; typedef struct LinkedList { void* head; /*SLnode if doubly_linked is false, otherwise DLnode*/ void* tail; /* here too */ bool doubly_linked; } LinkedList; And I want to access it like this: void* llnode_at(const LinkedList* ll, size_t index) { size_t i; SLnode* current; current = ll->head; for(i = 0; i <

Struct pointer casts

风格不统一 提交于 2019-12-24 04:32:01
问题 I'm trying to implement a linked list like this: typedef struct SLnode { void* item; void* next; } SLnode; typedef struct DLnode { void* item; void* next; struct DLnode* prev; } DLnode; typedef struct LinkedList { void* head; /*SLnode if doubly_linked is false, otherwise DLnode*/ void* tail; /* here too */ bool doubly_linked; } LinkedList; And I want to access it like this: void* llnode_at(const LinkedList* ll, size_t index) { size_t i; SLnode* current; current = ll->head; for(i = 0; i <

Swift Cast Generics Type

六月ゝ 毕业季﹏ 提交于 2019-12-24 04:04:28
问题 I'm trying to cast a generic type to its super class. class Foo : NSObject { } class Test<T> { let value: T init(_ value: T) { self.value = value } } let c = Test(Foo()) let v = c as Test<AnyObject> But on the line let v = c as Test<AnyObject> I get 'Foo' is not identical to 'AnyObject' While I can do that with built-in Arrays let array1 = [Foo(), Foo()] let array2 = array1 as [AnyObject] 回答1: Arrays are special-cased in Swift to be able to have this behaviour. You won't be able to replicate

Java: Casting to Generic with Interface Pointers

人走茶凉 提交于 2019-12-24 03:07:32
问题 In the following sample code two classes EventA and EventB both implement the interface Historical . Java can automatically cast an EventA or EventB to Historical when one of these objects is passed as a parameter, as in the examineEvent method below. However, Java is no longer able to cast when a generic is introduced ie. from List<EventA> to List<Historical> -- Unless the target function (in this case findClosestValidEventIndex ) is declared using List<? extends Historical> . Can someone

MFC DYNAMIC_DOWNCAST vs. dynamic_cast

天大地大妈咪最大 提交于 2019-12-24 03:02:28
问题 What is the difference between DYNAMIC_DOWNCAST from MFC library and standard C++ dynamic_cast operator? Can I use safety dynamic_cast instead of DYNAMIC_DOWNCAST for MFC objects? When my classes contain DECLARE_DYNAMIC and IMPLEMENT_DYNAMIC macros, can I use dynamic_cast operator or I must use DYNAMIC_DOWNCAST macro for this type of objects? 回答1: What is the difference between DYNAMIC_DOWNCAST from MFC library and standard C++ dynamic_cast operator? DYNAMIC_DOWNCAST and dynamic_cast achieve

Why does dynamic cast from class to subclass requires the class to be polymorphic?

孤人 提交于 2019-12-24 02:59:12
问题 As I understand it, what makes dynamic cast different from a static cast is its use of RTTI, and the fact that it fails if the dynamic type of a variable- when casting from base to derived- does not fit. But why does the class have to be polymorphic for that to be done if we have the RTTI anyway? EDIT: Since there was some confusion about the use of the word "polymorphic", here's the entry in cplusplus.com that prompted me to ask this: dynamic_cast can be used only with pointers and

How to fix: error: invalid conversion from 'const MyClass*' to 'MyClass*'

落花浮王杯 提交于 2019-12-24 02:57:30
问题 I am getting this compile error: error: invalid conversion from 'const MyClass*' to 'MyClass*' Here is the code: std::tr1::shared_ptr<MyClass> myClassA; const MyClass* myClassB; myClassA = std::tr1::shared_ptr<MyClass>(myClassB); // error here I think I understand the error, just don't know how to fix. I need myClassB to be a const so how to convert/copy classB to a shared_ptr? 回答1: You'll need a shared pointer to a const object: std::tr1::shared_ptr<const MyClass> myClassA; ^^^^^ 回答2: You

Primitive cast and assignments in Java

对着背影说爱祢 提交于 2019-12-24 02:27:16
问题 Why the following works float f = 10l // OK 32 bits to 64 ? float is only 32 bits and long 64 bits so there is NO room for float in the long and this one doesn't long l = 10f //compile error Could someone explain to me how kind of casting works? 回答1: The conversion from long to float is specifically allowed for assignment by the JLS, Section 5.1.2: 19 specific conversions on primitive types are called the widening primitive conversions: byte to short, int, long, float, or double short to int,

Is it safe to cast void pointer to char pointer pointer

久未见 提交于 2019-12-24 02:23:26
问题 Just wondering if it's safe to cast like this: char **cpp; // ... allocation and what not void *vp = (void *)cpp; // ... cpp = (char **)vp; should a void ** be used or is void * fine? This works on a couple of my boxes without issue but was wondering if it could cause problems on certain systems. 回答1: The cast is always safe, dereferencing it is safe as long as the pointer is valid. The only case where you'd use a void ** is when you plan to dereference it to get a void * . However, unless