简体   繁体   English

以某种方式将基类的指针转换为未知子类的指针?

[英]casting pointer of base class to unknown subclass possible somehow?

Scenario : 场景

BaseClass* pointer = &subClassXObject;  // subClassXObject is object of class subClassX 
                                        // which is derived from BaseClass and 
                                        // Instance<subClassX>. (Instance<subClassX> is 
                                        // for counting instances of type subClassX.)
                                        // Instance<subClassX> is derived from 
                                        // BaseInstance.

&... stands for getting an address, C++ Standard terminology might define object as being the address already, in this case &... is the object itself &...代表获取地址,C ++标准术语可能已经将对象定义为地址,在这种情况下&...是对象本身

The class of subClassXObject is unknown at this point, it can be any defined subclass of BaseClass. 此时尚不知道subClassXObject的类,它可以是BaseClass的任何已定义子类。

It is known that it is one of the direct derived subclasses of BaseClass which each also inherit from Instance<subClassX> where subClassX is the name of the direct derived subclass of BaseClass. 众所周知,它是BaseClass的直接派生子类之一,每个子类也都继承自Instance <subClassX>,其中subClassX是BaseClass的直接派生子类的名称。

Instance<subClassX> inherits from BaseInstance. Instance <subClassX>继承自BaseInstance。 BaseInstance defines a public pure virtual function which Instance<subClassX> implements, it's name is getDescriptor. BaseInstance定义了Instance <subClassX>实现的公共纯虚函数,名称为getDescriptor。

Thus every subClassObject has access to getDescriptor. 因此,每个subClassObject都可以访问getDescriptor。

SubClassX* pointer = &subClassXObject;
pointer->getDescriptor();               // This should work

(Or does it not? Now I am riddling: do I have to explicitly implement a base class public method in a subclass in order for someone being able to call it upon the subclass from outside? No..., haven't I?). (或者不是吗?现在我开玩笑了:我是否必须在子类中显式实现基类的public方法,以便有人可以从外部对子类进行调用?不是,不是吗? )。

I like 我喜欢

BaseClass* pointer = &subClassXObject;
pointer->getDescriptor();

to work. 上班。

Moving getDescriptor from BaseInstance and Instance<subClassX> to BaseClass and each sub class is possible, however I want users of my BaseClass to only write application logic code, not infrastructure code (even if they only would have to return an Instance<subClassX> methods return value). 可以将getDescriptor从BaseInstance和Instance <subClassX>移至BaseClass以及每个子类,但是我希望BaseClass的用户仅编写应用程序逻辑代码,而不编写基础结构代码(即使他们只需要返回Instance <subClassX>方法)返回值)。 The idea behind this is to create an object browser into which every sub classes instantiated object is fed and listed without users having to add any non-classpurpose-code. 其背后的想法是创建一个对象浏览器,在该浏览器中,每个子类实例化的对象都将被馈入并列出,而用户不必添加任何非类目的代码。

Casting pointer to BaseInstance* crashes the application. 将指针转换为BaseInstance *会使应用程序崩溃。 Thus my question as phrased in the title . 因此,我的问题如标题中所述。

Thank you for your caring! 谢谢您的关心!

Just define the function in base class with virtual key word, and the right derived class will be pickup automatically. 只需使用虚拟关键字在基类中定义函数,然后将自动拾取正确的派生类。

class Base{
public:
virtual int getDescriptor();
};

Declare BaseClass* pointer as BaseInstance* pointer. 将BaseClass *指针声明为BaseInstance *指针。 Then a cast is not necessary and it works. 然后,强制转换是没有必要的,并且可以使用。

You can only use BaseInstance methods then as casting to BaseClass* would crash then. 您只能使用BaseInstance方法,因为强制转换为BaseClass *会崩溃。 As the methods are used from the object browser this is about infrastructure functions, they make more sense in BaseInstance than in BaseClass, so not being able to use BaseClass methods is fine (BaseClass best contains base application logic methods only). 由于从对象浏览器中使用的方法与基础结构功能有关,因此它们在BaseInstance中比在BaseClass中更有意义,因此不能使用BaseClass方法就可以了(BaseClass最好仅包含基本应用程序逻辑方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM