What I'm actually trying to do is cast a constructed moneypunct
to the punct_facet
in this question without writing a copy constructor as in this answer.
But in the interests of writing a Minimal, Complete, Verifiable Example let's say that I have these two 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 a Parent
or Child
with the default constructor but that won't setup Complex members
. So say that I am given Parent foo
which was constructed using the custom constructor and I want to use the foo
object just with Child
's func
method. How do I do that? The straight up dynamic_cast<Child*>(&foo)
segfaults, so there may not be a way: http://ideone.com/JcAaxd
auto bar = dynamic_cast<Child*>(&foo);
Do I have to make a Child
constructor that takes a Parent
and internally copies it? Or is there some way to cast bar
into existence?
EDIT:
To give insight into my actual problem, the Parent
from the example is moneypunct
, which is implemented in the standard so I cannot modify it.
class punct_facet
is mine and the Child
from the example, it inherits moneypunct
and if I'm trying to stay implementation independent I cannot even internally use moneypunct
's member variables.
Which means that I must data mirror all of moneypunct
member variables in punct_facet
and copy construct them on punct_facet
construction. This results in an object that's twice as fat as it needs to be and requires me to reimplment all moneypunct
functionality.
Clearly that's undesirable, but the only way I can find around it is to take a previously constructed moneypunct
and treat it as a punct_facet
as requested by this question.