downcasting

How can I correctly downcast the pointer from void* to TMemo* in C++Builder2009?

Hello, I am writing multi-thread socket chat in C++Builder 2009. It is almost complete in accordance with what I need to do but I have a little problem. I need to pass the TMemo* pointer into CreateThread WinAPI function which upcasts it to void*. I tryed this way: HANDLE xxx = MemoChat->Handle; hNetThread = CreateThread(NULL, 0, NetTh...

Downcasting in C#

I'm facing a problem that I don't know how to solve and am hoping the community can help. I'm writing an app that manages "Lead" objects. (These are sales leads.) One part of my program will import leads from a text file. Now, the text file contains lots of potential leads, some of which I will want to import and some of which I won't. ...

Base object in constructor as alternative to downcast

Hi, I have a list of base objects (RTUDevice) and want to iterate through and convert each to a derived object (actually a derived of a derived RTDSensor) , however the downcasting is throwing an error. public RTUDevice(int id) { _id = id; } public class RTDDevice : RTUDevice { public RTDDevice(int id) : base(id) {...

downcasting in php5

I've realized that there's no downcasting in php5. Is there a common pattern to achieve it? ...

how expensive is this php downcasting workaround

This question is related to http://stackoverflow.com/questions/1721949/downcasting-in-php5 How expensive is this php downcasting workaround? Is this php downcasting workaround too expensive? I've echoed microtimes and It seems that it takes like 0.001. I wonder if It could be a problem in a large foreach. public static function to(...

Is downcasting (i.e. casting to derived type) ALWAYS wrong?

What is your perspective on downcasting? Is it ALWAYS wrong, or are there cases where it is acceptable, or even preferable or desired? Is there some good measure/guideline we can give that tells us when downcasting is "evil", and when it's "ok"/"good"? (I know a similar question exists, but that question spins out from a concrete case....

How to do dynamic downcasting in vb.net?

I have several classes, that all derives from SuperClass. When the classes are created, they all are put into a List(Of SuperClass). When I go through the list, i would like to downcast the SuperClass object to its baseObject, and put it into the correct list. (I have one list created for each of the sub types of SuperClass). It is p...

How to properly downcast in C# with a SWIG generated interface?

I've got a very large and mature C++ code base that I'm trying to use SWIG on to generate a C# interface for. I cannot change the actual C++ code itself but we can use whatever SWIG offers in the way of extending/updating it. I'm facing an issue where a C++ function that is written as below is causing issues in C#. A* SomeClass::next(...

Are there any C++ tools that detect misuse of static_cast, dynamic_cast, and reinterpret_cast?

The answers to the following question describe the recommended usage of static_cast, dynamic_cast, and reinterpret_cast in C++: http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-and-reinterpret-cast-be-used Do you know of any tools that can be used to detect misuse of these kinds of cast? Would a static ana...

Is this not downcasting?

If I do double d = 34.56; int i = (int)d; Am I not "downcasting"? OR Is this term only used in terms of classes and objects? I am confused because in this case we are "downcasting" from a bigger double to a smaller int, but in case of classes, we "downcast" from a smaller base class to a bigger derived class. Aren't these two c...

Java downcasting and is-A has-A relationship

HI, I have a down casting question, I am a bit rusty in this area. I have 2 clasess like this: class A{ int i; String j ; //Getters and setters} class B extends A{ String k; //getter and setter} I have a method like this, in a Utility helper class: public static A converts(C c){} Where C are objects that are retireved from the dat...

Shallow copying a list with downcasting

I have the class herichary as follows CEntity---->CNode--->CElement I have a class Nodes : List<Cnode> and Class Elements : List<Element> Node class contain common item common across different project Element class has item specific to a project. I have to shallow copy the element list into the node list (basically down casti...

Trying to understand how the casting/conversion is done by compiler,e.g., when cast from float to int

When a float is casted to int, how this casting is implemented by compiler. Does compiler masks some part of memory of float variable i.e., which part of memory is plunked by compiler to pass the remaining to int variable. I guess the answer to this lies in how the int and float is maintained in memory. But isn't it machine dependent...

How can I do a safe downcast and prevent a ClassCastException

Hi, I have the following scenario: public class A { } public class B extends A { } public class C extends B { public void Foo(); } I have a method that can return me class A, B or C and I want to cast safely to C but only if the class I get is of type C. I need to call Foo() but I don't want the ClassCastException. ...

Cant copy construction be done without creating an explicit function in the pure virtual base class?

My objective is to do a deep copy of a class, but a virtual class is causing trouble. #include<iostream> using namespace std; class Vir//pure virtual class { public: virtual void hi()=0; }; class Handler:public Vir { public: int i; Handler() {} Handler(int val):i(val) {} void hi() {cout<<"Value of i="<<i<<e...

How do I down-cast a c++ object from a python SWIG wrapper?

The problem: I've wrapped some c++ code in python using SWIG. On the python side, I want to take a wrapped c++ pointer and down-cast it to be a pointer to a subclass. I've added a new c++ function to the SWIG .i file that does this down-casting, but when I call it from python, I get a TypeError. Here are the details: I have two c++ cl...

Operator-function + with two implicit casts doesn't work

hi, I'm trying to port some parts from ginac (www.ginac.de) to C#. But I encountered this: class Program { static void Main(string[] args) { symbol s = new symbol(); numeric n = new numeric(); ex e = s + n; // "Operator + doesn't work for symbol, numeric" } } class ex { //this should be alre...

Downcasting a generic type in C# 3.5

Why can I only upcast a generic and not downcast it? How is it not clear to the compiler that if my constraint says where T : BaseClass and U is derived from BaseClass that (U)objectOfTypeT is valid? ...

static_cast on derived classes when base turns from not polymorphic to polymorphic

Hi All I am reviewing C++ casts operator and I have the following doubt: for polymorphic classes I I should use polymorphic_cast I should never use of static_cast since down-casting might carry to undefined behavior. The code compiles this case anyway. Now suppose that I have the following situtation class CBase{}; class CDerive...

Why can't I downcast pointer to members in template arguments?

If I make a pointer-to-base-member, I can convert it to a pointer-to-derived-member usually, but not when used within a template like Buzz below, where the first template argument influences the second one. Am I fighting compiler bugs or does the standard really mandate this not work? struct Foo { int x; }; struct Bar : public Foo ...