method syntax for handle objects

조회 수: 2 (최근 30일)
Jason
Jason 2021년 10월 10일
편집: Matt J 2021년 10월 19일
For handle class methods, would there be any performance difference between the two implementation below?
function initializeMesh(obj,tempVertices,tempFaces)
end
function obj = initializeMesh(obj,tempVertices,tempFaces)
end
I know the lower of the two is a little more flexible syntatically in that both:
meshInstance.initializeMesh(tempVertices,tempFaces)
and
meshInstance = meshInstance.initializeMesh(tempVertices,tempFaces)
are allowed and seem to produce the same result. I'm leaning towards include the obj return for that reason. Is there anything I'm missing here that might make the returnless method better?

채택된 답변

Matt J
Matt J 2021년 10월 11일
편집: Matt J 2021년 10월 11일
Well, the second syntax is the one you would normally use if the method is intended to return a new, freshly constructed, independent instance of the class. If that's not the case, then future developers of your code might look at it and mistakenly assume that this,
meshInstance2 = meshInstance1.initializeMesh(tempVertices,tempFaces)
will result in separate instances of the class meshInstance1 and meshInstance2.That can be a dangerous mistake to make. In later lines of code, you think that manipulations of meshInstance2 are not affecting meshInstance1, when in fact they are.
  댓글 수: 2
Jason
Jason 2021년 10월 18일
That is certainly a good point, thanks Matt
Matt J
Matt J 2021년 10월 19일
편집: Matt J 2021년 10월 19일
You are welcome, but if my answer has resolved your question, please do Accept-click it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by