-3

I have a parent-child inheritance structure. I have an object that was instantiated(new) by parent class. I want to downcast this object to child class. I need a automatic routine like casting because parent class has a lot of properties and copying parent properties in child object is impossible.

I can cast parent object to child object with reinterpret_cast operator so that I have parent properties values in my child object but I encountered with other problem.

After downcasting if you assign memory to one of child specific variables , when you want to delete child object you will faced with memory segmentation fault error. it seems heap was corrupted.

my code is similar to this :

    class parentclass
    {
    public:
        int parent_var = 10;
        parentclass()
        {
            parent_var = 20;
        }

    };
    class childclass :parentclass
    {
    public:

        int* child_var;

        childclass()
        {

            child_var = NULL;
        }
    };
void main()
{
        parentclass* pobj = new parentclass();
        childclass* cobj;
        cobj = reinterpret_cast<childclass*>(pobj);
        //everything is ok, cobj.parent_var has correct value (=20)
        //and child specific variables are filled with random variables. 

        delete cobj;
        // cobj delete successfully


        parentclass* pobj2 = new parentclass();
        childclass* cobj2;
        cobj2 = reinterpret_cast<childclass*>(pobj2); 
        //everything is ok   and   
        //cobj2.parent_var has correct value

        cobj2->child_var = new int[10]; // assign memory to child specific variable

        delete cobj2;  // Here Heap corruption Error occurred.
}

I read similar pages in stackoverflow but most of them describe casting when object new with childclass. your helps are appreciated.

melpomene
  • 84,125
  • 8
  • 85
  • 148
hamed
  • 471
  • 1
  • 9
  • 22
  • 1
    C++ is not C and C has no classes. – melpomene Jun 13 '18 at 07:51
  • 1
    You use an object of `parentclass` as it is an object of `childclass`. The other way would be okay. Your code invokes undefined behaviour. It is likely that `cobj2->child_var = new int[10];` overwrites some internal memory, which is used when you delete the object. – mch Jun 13 '18 at 07:52
  • 1
    That is not how inheritance works. When `new` creates the object it allocates enough memory to store only its elements. It know nothing about derived classes. Consider creating child object instead. Also using `static_cast` to cast the object to the wrong type yields undefined behavior. – Teivaz Jun 13 '18 at 07:53
  • 3
    What you're doing is not a dowcast. A downcast is when you have a pointer of type `parentclass *` that's actually pointing to an object of type `childclass`, and you want to get a `childclass *`. What you have is an object of type `parentclass`, and you're lying to the compiler about its type. This cannot work. – melpomene Jun 13 '18 at 07:53
  • 1
    For the sake of adding some theory to this discussion, you should have a look at the [Liskov substitution principle](https://en.wikipedia.org/wiki/Liskov_substitution_principle) to get a better understanding of the matter. – andreee Jun 13 '18 at 07:59

1 Answers1

0

you shouldn't use reinterpret_cast in this case(and try to avoid it in general) because reinterpret_cast is just changing the interpretation of the data it doesn't perform any thing else then changing the type

Which means that cobj is still pointing to the same data as pobj. This is why your code fail when you try to modify child_var

You can only downcast a pointer if it was created as the child, what I mean is

parentclass* pobj = dynamic_cast<parentclass*>(new childclass()); //created as a child not a parent
childclass* cobj;
cobj = dynamic_cast<childclass*>(pobj); //dynamic_cast instead of reinterpret_cast

would work if the parrent was public

class childclass : public parentclass

also

void main() should be int main()

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Tyker
  • 2,971
  • 9
  • 21
  • I understand casting is impossible as you said, now how I can solve my problem and have a child object which is filled with that parentobject values? – hamed Jun 13 '18 at 08:58
  • you don't need to cast to access the parent's members if you inherit publicly – Tyker Jun 13 '18 at 09:08
  • 1
    Downvoting because I don't believe this answers the questioner's true problem, which is that they're casting one type to a completely different type and expecting that to work for some reason. – James Picone Jun 14 '18 at 05:38
  • @JamesPicone i think i addressed this issue by explaining why he can't use reinterpret_cast and what he needed to do to use dynamic_cast – Tyker Jun 14 '18 at 07:12