dynamic-cast

Accessing subclass members from a superclass pointer C++

孤街浪徒 提交于 2019-12-12 08:13:56
问题 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 these functions are not overloaded, they are not found in Student, so the compiler kicks up a fuss. If I have a pointer to Student, is there a way to get a pointer

method chain a derived class method after calling a base class method

这一生的挚爱 提交于 2019-12-12 06:38:31
问题 With a class and a derivative as shown below, is there a way for the base classes methods to return a object reference of the derived type instead of its own type so syntactically i can chain the methods together? Suppose that object A has methods a1,a2 and derivative AD adds a method ad1 how would one go about making a valid method chain of AD_instance.a1().a2().ad1(); ? Below are the two classes in question. Ignoring what it's actually doing, the method chaining is the only important part.

How to specify “all parameterized types” or “all argument lists” for a dynamic_cast?

為{幸葍}努か 提交于 2019-12-12 04:21:38
问题 I'm trying to call a method on a derived class. The derived class has template parameters, but I don't know what they are. How do specify “all parameterized types” or “all argument lists” for a dynamic_cast ? Below is the MCVE, but here's the thrust of the problem: // Contrived, but close approximation DerivedOne<X> one; // Another one DerivedTwo<X,Y> two; // Maybe another one somewhere... DerivedTwo<X,Z> three; // Here's the problem Base& base = dynamic_cast<Base&>(two); if (base.HasOp()) {

Inheritence and usage of dynamic_cast

冷暖自知 提交于 2019-12-11 23:25:40
问题 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 void DoSomethingElse(){ ... } virtual void SpecialD1DoSomething{ ... } }; class Derived2 { public: Derived2(){} virtual ~Derived2(){} virtual void DoSomething(){ ... } virtual void

Forced necessity of dynamic_cast operator in C++

我只是一个虾纸丫 提交于 2019-12-11 12:19:32
问题 dynamic_cast is generally used when we have a base class pointer and want to downcast it to a derived class. For instance, class A { public: virtual void foo(); }; class B : public A { public: void foo(); }; main() { A* a = new B(); B* b = dynamic_cast<B*> (a); } But, the same can also be accomplised by using C-style cast: B* b = (B*)a; So, my question is what are the circumstances/cases where it becomes completely necessary to use this operator i.e. there is no other choice? 回答1: When you

ClassCastException thrown when calling .asSubclass from separate jar

纵饮孤独 提交于 2019-12-11 04:33:46
问题 I have a program through which I have the following three classes. These first two are in jar1.jar : Main (uses the jar loading trick found here): package prob1; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarFile; public class Main { static List<Class<?>> classes = new ArrayList<Class<?>>(); static String pathToJar = "/Users

Is it ok to dynamic cast “this” as a return value?

…衆ロ難τιáo~ 提交于 2019-12-10 19:26:41
问题 This is more of a design question. I have a template class, and I want to add extra methods to it depending on the template type. To practice the DRY principle, I have come up with this pattern (definitions intentionally omitted): template <class T> class BaseVector: public boost::array<T, 3> { protected: BaseVector<T>(const T x, const T y, const T z); public: bool operator == (const Vector<T> &other) const; Vector<T> operator + (const Vector<T> &other) const; Vector<T> operator - (const

Avoiding dynamic_cast for downcasting to the original type

爱⌒轻易说出口 提交于 2019-12-10 18:29:32
问题 How can I downcast safely (ie returning null on failure) to the exact type of the underlying object, without incurring the performance penalty of dynamic_cast , and without having to put support code in every class I use? 回答1: dynamic_cast will traverse the entire inheritance tree to see if the conversion you want is possible. If all you want is a direct downcast to the same type as the object, and you don't need the ability to cross cast, to cast across virtual inheritance, or to cast to a

The reason why dynamic_cast doesn't work with non-polymorphic types

老子叫甜甜 提交于 2019-12-10 14:17:39
问题 With classes B and derived class D : class B { int b; }; class D : public B { int d; }; D* d = new D(); B* b = dynamic_cast<B*>(d); the above will work fine—it's a simple upcasting. We're sure that whatever b is pointing at has the B class (sub-)object in it. However, B* b = new D(); D* d = dynamic_cast<D*>(b); won't compile even though b points to a valid D instance—because the base class is not polymorphic. So adding just one, empty virtual method would solve the problem. The important

“UnsatisfiedLinkError” appears when “dynamic_cast” is used in Android NDK

孤街醉人 提交于 2019-12-10 09:35:09
问题 I am new in the developing for Android and I faced with next problem: when I using C++ code that uses "dynamic_cast" expressions - "UnsatisfiedLinkError" appears when I am starting my application on an emulator. But when I run application without it - all works OK( I mean without any errors to LogCat ) I tried to run it on Android 2.3.3. I used android-ndk-r7b. My Application.mk: APP_OPTIM := debug APP_ABI := armeabi APP_STL := gnustl_static APP_MODULES := native_lab My Android.mk: LOCAL_PATH