Can an object pass a handle to itself?

3 ビュー (過去 30 日間)
Bruce Elliott
Bruce Elliott 2019 年 8 月 23 日
編集済み: Matt J 2019 年 8 月 23 日
Can an object of a handle class pass its own handle to a function, so that the function has access to certain properties of the object? I believe this would be similar to use of the 'this' pointer in C++.
Here's what I'm trying to do, in case there's a better way to accomplish it:
I have class A, instances of which will create child objects that are instances of another class B. Objects of class B will only ever exist as children of class A objects, and their behavior will depend on the current values of some their parent's properties. Thus I'd like them to have the handle to their parent as one of their properties.
Thus, I'd like the parent to be able either to set the child's property directly or to pass a handle to the class B constructor.
The only easy (but tedious) alternative I see is to have the parent explicitly pass the current values of any required properties to the methods of the child when it calls them.
Suggestions?
Thanks.

採用された回答

Matt J
Matt J 2019 年 8 月 23 日
編集済み: Matt J 2019 年 8 月 23 日
Yes, you can pass an instance of A to the constructor of each child B. And, if A is a handle class, this will provide each child with pointer-like access to the property values of A.
  2 件のコメント
Bruce Elliott
Bruce Elliott 2019 年 8 月 23 日
Right after I submitted this question, I realized that the 'obj' variable that I routinely use in my class methods is essentially a "this" pointer, isn't it? I should be able to pass that to the constructor of my child object, right?
Matt J
Matt J 2019 年 8 月 23 日
編集済み: Matt J 2019 年 8 月 23 日
I should be able to pass that to the constructor of my child object, right?
Yes.
Also, if the properties of A are read-only, you don't even need to make A a handle class. It can be a value class instead and still be stored by all of its children.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConstruct and Work with Object Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by