c++ - Alternative to casting return pointer of base class protected method? -


i came class uses protected nested struct, intending derived classes augment struct. end, declared virtual method allocating struct.

now, base class not-trivial amount of work in processsomedata , derived class reuse it.

which leads following:

class { public:     virtual void doprocessing(); // uses processsomedata()  protected:     struct somedata     {         virtual ~somedata() {};          // data members     };      virtual somedata* processsomedata(); // uses allocatesomedata()     virtual somedata* allocatesomedata(); };  class b : public { public:     virtual void doprocessing()     {         deriveddata* mydata =             static_cast<deriveddata*>(a::processsomedata()); // *** seems little suspect          // work on additional data members in mydata     }  protected:     struct deriveddata : public somedata     {         // more data members     };      virtual deriveddata* allocatesomedata(); }; 

because allocatesomedata overridden, know a::processsomedata returning somedata* pointing deriveddata, static_cast safe.

that said, feels little off should have cast base derived @ all, when else seems pretty kosher.
there better/proper way this, without using cast? or have re-design classes/structs?

it because compiler doesn't know sure processsomedata uses allocatesomedata create somedata struct. far compiler knows somedata, returned processsomedata might instance of somedata. deriveddata somedata not other way round.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -