Object returning its field possible?
4 views (last 30 days)
Show older comments
Robert Borkowski
on 5 Aug 2022
Commented: Robert Borkowski
on 9 Aug 2022
Hi,
I am writing a class. Is there a way to return one of object's fields, instead of the object itself, when calling it? In other words, I want that my object becomes indistinguishable from double for all practical applications. I want to do this to avoid the need to overload all operators, and ensure all legacy code, which assumes that variables are of type double, can be used without any changes, and can remain compatible with both types.
The closest way I can think of is to overload the uplus operator, which for my class would return one of the fields, while having no effect on double. This will make the code transparent to variable type, but still requires code changes (unary + has to be added in front of each occurence of such variable) and will not be compatible with functions I cannot change e.g., built-in functions. Is there a better way?
Thanks!
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Construct and Work with Object Arrays 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!