It’s been a while since I have had to write C++ code and I’m feeling kind of stupid. I’ve written code that is similar to, but is not exactly, the code below:
class Parent
{
...
};
class Child : public Parent
{
...
};
class Factory
{
static Parent GetThing() { Child c; return c; }
};
int main()
{
Parent p = Factory::GetThing();
Child c1 = p; // Fails with "Cannot convert 'Parent' to 'Child'"
Child c2 = (Child)p; // Fails with "Could not find a match for 'TCardReadMessage::TCardReadMessage(TCageMessage)'"
}
I know this is supposed to be simple but I’m not sure what I’m doing wrong.
A
Parentobject returned by value cannot possibly contain anyChildinformation. You have to work with pointers, preferably smart pointers, so you don’t have to clean up after yourself: