Jump to content

Can a parent class function access the data of the child class?


Recommended Posts

I know this question sounds strange and is probably not well phrased. So here is my problem:

I have a parent class Setup.lvclass. From this I inherit multiple classes, e.g. LivSetup.lvclass, TlmSetup.lvclass, etc.

The parent class has three dynamic dispatch functions: ReadFromIniFile.vi, WriteToIniFile.vi, WriteToConfigFile.vi

All classes have as their private data a cluster called Setup Data and a string called Ini File Section.

Now currently I have an override function in each child class for each of the three dynamic dispatch functions. This allows me to bundle up my child classes into an array and run a for loop on the dispatch functions.

post-1022-1218727400.png?width=400

This part works really well.

Now I noticed that my ReadFromIniFile.vi and WriteFromIniFile.vi are identical for all child classes. The only difference being the contents of the Setup Data cluster.

post-1022-1218727392.png?width=400

So my thought was to implement the same code in the base class and remove the overrides. Now the problem is that in order to make the code work in the base class I have to give it the same controls in the private data. Therefor now the functions always save that data to the ini file regardless of what child class I wire to it.

Is there any way around that? Can I cast the data dynamically to the data type of the child class that is wired to it? Or am I just completely misusing the inheritance here ;-)

Any help is appreciated. For now I will continue with the identical overrides.

Cheers,

Heiko

Link to comment

You can't cast a parent as a child unless the true class type of the object is in fact a child (having been previously up-casted).

Your solution is to implement the code in the parent and then call it within the child, so logically the opposite to what you're questioning.

Inheritance works in one way only.

Why not remove the data from the children and simply use the parent implementation (Call parent method)?

Shane.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.