0

Suppose I have the following class:

classdef myClass < handle
    properties
        A = [10 20 30 40]
    end
end 

Then suppose I have the following calls:

>> m = myClass;
>> n = m;

n will be a handle to the same object as m is, of course. Is there a way to overload the assignment operator? In particular, I wonder if I can have something like the following method:

function val = assign(obj)
    val = obj.A;
end

So doing n = m would act the same as n = m.A.

2 Answers 2

2

I don't think you can: just imagine how you would assign the object itself to a variable, it would be the exact same syntax. And since you can't have ambiguities for a program to be executable, it is very unlikely to be possible and useful.

If you'd just want it as syntactic sugar, learn to live with the limitations. Otherwise you might want to take another look at your design.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not possible to overload the = operator to do this.

But (as you probably realised) there's no reason why you can't implement your assign method as you have done, and then call n = assign(m).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.