c++ - Cast to a Child -


what i'm trying cast constructed moneypunct punct_facet in this question without writing copy constructor in this answer.

but in interests of writing minimal, complete, verifiable example let's have these 2 classes:

class parent{ public:     parent(complex& args);     parent operator=(const parent&) = delete;     parent(const parent&) = delete;     parent() = default;      virtual void func(); private:     complex members; };  class child : public parent{ public:     virtual void func(); }; 

i can construct parent or child default constructor won't setup complex members. given parent foo constructed using custom constructor , want use foo object child's func method. how do that? straight dynamic_cast<child*>(&foo) segfaults, there may not way: http://ideone.com/jcaaxd

auto bar = dynamic_cast<child*>(&foo); 

do have make child constructor takes parent , internally copies it? or there way cast bar existence?

edit:

to give insight actual problem, parent example moneypunct, implemented in standard cannot modify it.

class punct_facet mine , child example, inherits moneypunct , if i'm trying stay implementation independent cannot internally use moneypunct's member variables.

which means must data mirror of moneypunct member variables in punct_facet , copy construct them on punct_facet construction. results in object that's twice fat needs , requires me reimplment moneypunct functionality.

clearly that's undesirable, way can find around take constructed moneypunct , treat punct_facet requested question.

it wouldn't work think, since have made function func virtual. means if convert pointer parent pointer child, func() of object still parent::func().

now, theoretically this:

#include <iostream>  class parent { public:         virtual void foo() { std::cout << "parent" << std::endl; } };  class child : public parent { public:         virtual void foo() { std::cout << "child" << std::endl; } };  int main() {         child child;         child.foo(); // "child"         child.parent::foo(); // "parent"         parent parent;         parent.foo(); // "parent"         ((child*)(&parent))->foo(); // still "parent"         ((child*)(&parent))->child::foo(); // "child"         return 0; } 

and while may receive downvotes posting broken code, think necessary show happening in case. need convert both, pointer object, , specify function intending call.

depending upon doing, may better accomplished using friend classes:

#include <iostream>  class parenthelper; class childhelper; class parent {     friend class parenthelper;     friend class childhelper; private:     int a=5; };  class parenthelper { public:     virtual void func(parent *p)     {         std::cout << "parent helper, see " << p->a << std::endl;     } };  class childhelper : public parenthelper { public:     virtual void func(parent *p)     {         std::cout << "child helper, see " << p->a << std::endl;     } };  void foo(parent* p, parenthelper *h) {     h->func(p); }  int main() {     parent p;     parenthelper ph;     childhelper ch;      ph.func(&p);     ch.func(&p);      foo(&p, &ph);     foo(&p, &ch);      return 0; } 

note several things:

  1. friendships not inherited, must list children parenthelper intend use.
  2. it does, however, give way access data members of parent class is, won't cause weird behaviour.
  3. this may still not looking for, question think may help.

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -