dynamic-cast

Is there a way to do dynamic implicit type casting in C#?

Given this class with an implicit cast operator: public class MyDateTime { public static implicit operator MyDateTime(System.Int64 encoded) { return new MyDateTime(encoded); } public MyDateTime(System.Int64 encoded) { _encoded = encoded; } System.Int64 _encoded; } I can now do the following...

java: how can i do dynamic casting of a variable from one type to another?

Hiya. i would like to do dynamic casting for a java variable, the casting type is stored in a different variable. this is regular casting: String a = (String) 5; this is what i want: String theType = 'String'; String a = (theType) 5; is it possible? and if so how? thanks! update I'm trying to populate a class with a hashMap ...

Template deduction in dynamic_cast

I have a class that is defined as the following: template <class WidgetType> class CometWidget : public WidgetType; Inside a function I am doing this: dynamic_cast<CometWidget *>(iter2->second.second)->changesCommited_(); and it resolves the CometWidget type, complies and run correctly. The code runs inside the CometWidget class....

dynamic_cast in c++

As i am new to c++ ,i am quite confused with the dynamic_cast keyword in c++. struct A { virtual void f() { } }; struct B : public A { }; struct C { }; void f () { A a; B b; A* ap = &b; B* b1 = dynamic_cast<B*> (&a); // NULL, because 'a' is not a 'B' B* b2 = dynamic_cast<B*> (ap); // 'b' C* c = dy...

dynamic_cast fails when used with dlopen/dlsym

Intro Let me apologise upfront for the long question. It is as short as I could make it, which is, unfortunately, not very short. Setup I have defined two interfaces, A and B: class A // An interface { public: virtual ~A() {} virtual void whatever_A()=0; }; class B // Another interface { public: virtual ~B() {} virtual voi...

Any suggestion for doing an arbitrary operation using given arguments of arbitrary types?

Basically i just want to do an arbitrary operation using given arguments of arbitrary types. Argument type base class is Var, and Operation is base class of the operation that will executed for given arguments. I have Evaluator class, that hold a collection of operators which mapped using opId. Evaluator will do operation based on opId...

Dynamically Casting Objects

Hello everyone I am trying to cast two objects to, both, a specific type based on property reflection information. I want to do this dynamically so I don't need a bunch of switch cases and such for each type that the two objects can be in this class. Overall they will mostly be int or float. At the moment I currently tried using 'var' an...

Polymorphism problem: How to check type of derived class?

Hi, this is my first question here :) I know that I should not check for object type but instead use dynamic_cast, but that would not solve my problem. I have class called Extension and interfaces called IExtendable and IInitializable, IUpdatable, ILoadable, IDrawable (the last four are basicly the same). If Extension implements IExten...

Accessing subclass members from a superclass pointer C++

I have an array of custom class Student objects. CourseStudent and ResearchStudent both inherit from Student, and all the instances of Student are one or the other of these. I have a function to go through the array, determine the subtype of each Student, then call subtype-specific member functions on them. The problem is, because thes...

Is my method for avoiding dynamic_cast<> faster than dynamic_cast<> itself ?

Hi, I was answering a question a few minutes ago and it raised to me another one: In one of my projects, I do some network message parsing. The messages are in the form of: [1 byte message type][2 bytes payload length][x bytes payload] The format and content of the payload are determined by the message type. I have a class hierarchy...

typeid , dynamic casting (upcast) and templates

Hello, I have few questions regarding dynamic casting , typeid() and templates 1) How come typeid does not require RTTI ? 2) dynamic_cast on polymorphic type: When I do downcast (Base to Derive) with RTTI - compilation passes When RTTI is off - I get a warning (warning C4541: 'dynamic_cast' used on polymorphic type 'CBase' with /...

Changing type of object in a conditional

I'm having a bit of trouble with dynamic_casting. I need to determine at runtime the type of an object. Here is a demo: #include <iostream> #include <string> class PersonClass { public: std::string Name; virtual void test(){}; //it is annoying that this has to be here... }; class LawyerClass : public PersonClass { public: vo...

Inheritence and usage of dynamic_cast

Hello, Suppose I have 3 classes as follows (as this is an example, it will not compile!): class Base { public: Base(){} virtual ~Base(){} virtual void DoSomething() = 0; virtual void DoSomethingElse() = 0; }; class Derived1 { public: Derived1(){} virtual ~Derived1(){} virtual void DoSomething(){ ... } virtual v...

isMemberOfClass vs comparing classes with ==

Is there any real difference between: id value; BOOL compare1 = [value isMemberOfClass:[SomeClass class]]; BOOL compare2 = [value class] == [SomeClass class]; to check if value is a SomeClass object? ...

Dynamic Cast: Class [] and String [] to Comparable []

Instead of trying to trying to put my problem into words, here's some code that demonstrates what I want to do: Class [] domains = { Integer.class, Double.class, String.class }; String [] stringValues = { "12", "31.4", "dog" }; Comparable [] comparableObjects = { null, null, null }; for (int i = 0; i < domains.length; i++) { // Tri...

Cannot dynamic_cast void* to templated class

The exact error I'm getting is: Cannot dynamic_cast 'object' (of type 'void*') to type 'class udDator(int)*' (source is not a pointer to a class) This is happening inside an overridden operator delete. I'm attempting to create a templated memory management class that can inherit into any other class, managing memory through references...

How to write own dynamic_cast

Hi, This have been asked in the interview. How to write own dynamic_cast. I think, on the basis of typeid's name function. Now how to implement own typid? I have no clue on it. ...

Avoiding dynamic_cast in implementation of virtual functions in derived class

Here is some sample code explaining what I am trying to achieve. Basically, I have an algorithm that depends on some basic operations available in a class. I have defined those operations in a pure abstract base class. I want to apply that algorithm to a variety of objects that provide those operations by deriving classes for the specif...

How is dynamic_cast typically implemented?

Is the type check a mere integer comparison? Or would it make sense to have a GetTypeId virtual function to distinguishing which would make it an integer comparison? (Just don't want things to be a string comparison on the class names) EDIT: What I mean is, if I'm often expecting the wrong type, would it make sense to use something lik...

Testing a c++ class for features

I have a set of classes that describe a set of logical boxes that can hold things and do things to them. I have struct IBox // all boxes do these { .... } struct IBoxCanDoX // the power to do X { void x(); } struct IBoxCanDoY // the power to do Y { void y(); } I wonder what is the 'best' or maybe its just 'favorite' idio...