I am coding an application and ran into an issue where I have created a function in the parent class that references an array also in the parent class. I then overwrite the array in a child class and run the parent function from the child class. I expected the function to use the overwritten array from the child class, but it seems to use the array from the parent class. I am rather confused by this, because this is how it works for simple data types like integers, but apparently not with objects?
Can anyone explain what is happening here and if I can make a workaround that doesn’t involve overwriting the function in every child class? Thank you in advance
I was wrong. I have tested with simple data types. It still uses the parent’s value. It just doesn’t throw an exception when I use simple data types that I haven’t assigned a value and just uses a standard value.
Then let me rephrase my question. Is there a way to make a parent function reference a variable that has been overwritten a by a child when called from the child object?
You need to redefine your function in every chidren.
After there is something I don’t know in JAVA. In c++ when you store your child as a parrent (which you do with parrent[] children) you need to declare your function as virtual so it uses the children version of the function and not the parent version.