dynamic-cast

Dynamic Casts or Function Overloads?

こ雲淡風輕ζ 提交于 2020-01-04 03:53:25
问题 Consider the following abstract class: class Abstract { public: // ... virtual bool operator==(const Abstract& rhs) const = 0; // ... }; Now suppose I'm creating multiple derived classes from this abstract class. However, each one uses a different algorithm when comparing with its own type, and a generic algorithm when comparing with any of the other derived classes. Between the following two options, which would be the better, more efficient option? Option A: class Derived : public Abstract

Are dynamic_casts safe to remove in production code?

两盒软妹~` 提交于 2020-01-01 04:46:45
问题 dynamic_cast s are slower, but they are safer than static_cast s (when used with object hierarchies, of course). My question is, after I've ensured in my debug code that all (dynamic) casts are correct, is there any reason for me not to change them to static_cast s? I plan on doing this with the following construct. (Btw, can you think of a better name than assert_cast ? Maybe debug_cast ?) #if defined(NDEBUG) template<typename T, typename U> T assert_cast(U other) { return static_cast<T>

C++ dynamic_cast - polymorphic requirement and downcasting

偶尔善良 提交于 2019-12-30 03:11:10
问题 In the following code, while constructing obj in case 1, we construct a derived class object too, but its member functions are just inaccessible to obj . So while downcasting (i.e., in case 2), using obj as source, we have the constructed derived in it already. Why would obj need to be polymorphic? If I confused you with my above description, why doesn't obj need to be polymorphic when upcasting, but while downcasting it does need to be polymorphic while using dynamic_cast ? class base {

dynamic_cast across a shared_ptr?

亡梦爱人 提交于 2019-12-29 05:20:44
问题 I have two classes A and B, B inherits from A. If I have a shared_ptr<A> object which I know is really a B subtype, how can I perform a dynamic cast to access the API of B (bearing in mind my object is shared_ptr, not just A? 回答1: If you just want to call a function from B you can use one of these: std::shared_ptr<A> ap = ...; dynamic_cast<B&>(*ap).b_function(); if (B* bp = dynamic_cast<B*>(ap.get()) { ... } In case you actually want to get a std::shared_ptr<B> from the std::shared_ptr<A> ,

Are there practical uses for dynamic-casting to void pointer?

情到浓时终转凉″ 提交于 2019-12-27 13:34:30
问题 In C++, the T q = dynamic_cast<T>(p); construction performs a runtime cast of a pointer p to some other pointer type T that must appear in the inheritance hierarchy of the dynamic type of *p in order to succeed. That is all fine and well. However, it is also possible to perform dynamic_cast<void*>(p) , which will simply return a pointer to the "most derived object" (see 5.2.7::7 in C++11). I understand that this feature probably comes out for free in the implementation of the dynamic cast,

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

本小妞迷上赌 提交于 2019-12-27 10:31:13
问题 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 that i received. this is the constructor: public ConnectParams(HashMap<String,Object> obj) { for (Map.Entry<String, Object> entry : obj.entrySet()) { try { Field f = this

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

删除回忆录丶 提交于 2019-12-25 04:02:27
问题 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: long a = 5; MyDateTime b = a; But NOT the following: long f = 5; object g = f; MyDateTime h = g; This gives a compile time: Cannot implicitly convert type 'object' to 'MyDateTime'. Makes sense to me. Now

What's the advantages of turning off RTTI from compiler setting?

有些话、适合烂在心里 提交于 2019-12-23 19:15:32
问题 By this(How expensive is RTTI?), it seems clear that dynamic casting is much expensive than static type comparison, but I wonder if it would be worth to turn off RTTI option in compiler option(VS2010, /GR-) I have no dynamic cast in my code(I replaced them with static cast). But does (/GR-) option do any other than emitting errors when using dynamic cast? Is there any memory or code optimization in there? Thanks in advance. 回答1: Straight from MSDN (emphasis mine): When /GR is on, the compiler

Is it bad design to use dynamic_cast in c++

萝らか妹 提交于 2019-12-23 04:52:59
问题 I have a class CAbstractNode and it has 5 derived classes out of 5, only 2 (special) need a method SetValue() and a member int nVal; //myFunction is virtual function of base(cAbstractNode) implemented in 2 special derived classes myFunction(CAbstractNode * obj, int val) { Derived_02 nodeObj = dynamic_cast<Derived_02*>(obj); if(res != NULL) { nodeObj->setValue(val); } //remaining code goes here... } //myFunction is virtual function implemented in remaining 3 derived classes ( setValue() is not

C++ what can make type_info::hash_code differs for two (supposedly) same objects

纵饮孤独 提交于 2019-12-22 14:42:10
问题 After trying to debug an unsuccessful dynamic downcasting, I eventually found that the reason probably is: type_info::hash_code for the type it is casted to is not the same depending where in the code it is called. type_info::name stays exactly the same though. Unfortunately I'm unable to reproduce the behavior in a minimal example since it appears in a quite entangled context. Therefore I'm essentially looking for clues of what can be the cause of such behavior, and consequently what