Jump to content

Dynamic Dispatch: Convert To Sibling


Recommended Posts

post-10325-1241537425.png?width=400

Red File = Red colored class in picture - is a file format of a KKD File

DP & For = Displacement and Force - is a processing method of a file but is not tied to a specific format.

Class Hierarchy

KKD File as Parent

Red File as Child of KDD File

DP & For as Child of KDD File

Problem

I want to select a file format e.g. Red File Load File Method then call a processing method e.g. DP & For Process Method

Essentially I have different file types AND I have different processing methods for files.

Both operate on KKD File Class (which contains the data) but I want to be able to choose the format AND processing method - not have the processing method tied to the file format or vice versa.

I tried to downcast to the parent then upcast to the processing method?

The above generates an error (due to dynamic dispatch I guess) but is there a way to do this?

Can I jump through the hierarchy like this?

Can I code the above somehow?

Or can anyone suggest a better class hierarchy?

I was trying to avoid a separate independent class as both classes operate on a File so I thought they should inherit from KKD File and be siblings?

Any suggestions?

Cheers

JG

Link to comment

QUOTE (jgcode @ May 5 2009, 05:47 PM)

Any suggestions?

Cheers

JG

No i don't think this can be done like that.

Both sibling classes could have totally different member data and methods so there's no way for the compiler to automatically know how to convert one to another, if it's even possible.

You'll need to write a converter VI to do the work for you. I don't think this can be automated in the way you want.

Casting down (towards parent) is fine but casting across or forward is not possible. Bear in mind the ACTUAL class type is important here, not the "assumed" type. Man I wish I knew the proper syntax to explain what I mean....

I'll leave it up to somebody else with more accurate terminology to avoid confusion.

Shane.

Ps You can have classes IN classes (as member objects: "File format" as an object and "Operations" as an object) so you could use one class to load the file and another class to perform the operations. This way you can mix to your heart's content. You'll soon realise the flexibility of this and start converting EVERYTHING to classes.....

Link to comment

Shane's suggestion to use the composite relationship is a good one.

You could make DP & For a class that is part of the KKD class (or even make it separate, but let's keep it contained inside KKD for now). As far as I can see there is really no reason for DP & For to be a child of KKD anyway. Then KKD can just pass itself as an input (not for dynamic dispatching, but as a regular input on a separate terminal) to a DP & For method, and KKD's children can use the method, too.

If things get much more complicated you may want to take a look at the Strategy pattern.

Paul

Link to comment

QUOTE (Paul_at_Lowell @ May 5 2009, 06:27 PM)

Shane's suggestion to use the composite relationship is a good one.

You could make DP & For a class that is part of the KKD class (or even make it separate, but let's keep it contained inside KKD for now). As far as I can see there is really no reason for DP & For to be a child of KKD anyway. Then KKD can just pass itself as an input (not for dynamic dispatching, but as a regular input on a separate terminal) to a DP & For method, and KKD's children can use the method, too.

If things get much more complicated you may want to take a look at the Strategy pattern.

Paul

^^^^^^

¦ ¦ ¦ ¦ ¦ ¦

See what I mean: "composite relationship" and "Strategy pattern".

Cool.

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.