Object returning its field possible?

3 vues (au cours des 30 derniers jours)
Robert Borkowski
Robert Borkowski le 5 Août 2022
Commenté : Robert Borkowski le 9 Août 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!

Réponse acceptée

Walter Roberson
Walter Roberson le 5 Août 2022
  1 commentaire
Robert Borkowski
Robert Borkowski le 9 Août 2022
Thank you! That's exactly what I was looking for.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by