Can I "cast" a value class object to a handle class object on the fly
Show older comments
Hi
I'm relatively new to OOP. I currently have a value class object. Specifically, it's a custom date class that I'm working on. So, most of the time it makes sense to have the object as a value class.
However, there are occasions where I would want a particular instance of an object to be viewed as a handle class. Specifically in my case this could happen where I have an object that is let's say the current date. Hence if current date changes, all objects linked to that object should update. This obviously sounds like a handle class object.
So the question is, is there a way that I can use my date value class and cast a specific instance of that object as a handle.
Hope I stated that clearly.
Regards, Phillip
Accepted Answer
More Answers (2)
Phillip
on 5 Nov 2014
2 Comments
Adam
on 5 Nov 2014
This does look useful and not something I have used before. I tend to default to using a handle class most of the time because I prefer them, but certainly they are not always the appropriate solution to a problem and I have had situations before where I wanted deep copies of handle class objects which can get very messy.
Matt J
on 5 Nov 2014
Nice. I wasn't aware of that feature.
You would use a converter method, I think
I don't think you can avoid writing a second class that is a handle version of the original class, if that's what you're asking.
Are you sure it makes sense to have a value class version in the first place? I didn't follow your explanation of that. What if you made it a handle class to begin with and then had a class method unshare() to make unlinked copies when you need them?
1 Comment
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!