简体   繁体   中英

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

The class of subClassXObject is unknown at this point, it can be any defined subclass of 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.

Instance<subClassX> inherits from BaseInstance. BaseInstance defines a public pure virtual function which Instance<subClassX> implements, it's name is getDescriptor.

Thus every subClassObject has access to 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?).

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). 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. 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. Then a cast is not necessary and it works.

You can only use BaseInstance methods then as casting to BaseClass* would crash then. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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