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...
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 ...
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....
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...
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...
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...
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...
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...
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...
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...
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 /...
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...
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...
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?
...
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...
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...
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.
...
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...
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...
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...