Object returning its field possible?

2 views (last 30 days)
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!

Accepted Answer

Walter Roberson
Walter Roberson on 5 Aug 2022
  1 Comment
Robert Borkowski
Robert Borkowski on 9 Aug 2022
Thank you! That's exactly what I was looking for.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!