pointers in object-oriented matlab programing

23 ビュー (過去 30 日間)
Nathan Bblanc
Nathan Bblanc 2020 年 8 月 26 日
コメント済み: Walter Roberson 2020 年 8 月 28 日
I want to create several objects that can "communicate with each other"
for instance, let's say i have a class called "child" and a class called "woman", and i want each child to have a property "mother" and each woman to have a property "children". these properties should contain some sort of pointer to the other objects, so that i can (for instance) add a function to the "woman" class named "feed_children" and i can go over all all her children and change some parameter.
is there a way to do this? i note here that i don't want to simply use some sort of structure, where the children are contained in a "struct" inside the "mother" object. i want all the objects to appear in the workspace and have independent existence, but to have some way of pointing at each other. i know that in many object oriented languages this is very easy to do
many thanks
i attach here my code if anyone is interested, the "TA_system" class is the mother (components are children) and a duct is a component.
  2 件のコメント
Walter Roberson
Walter Roberson 2020 年 8 月 27 日
MATLAB objects derived from "handle" class effectively use pointers.
Nathan Bblanc
Nathan Bblanc 2020 年 8 月 27 日
thank you for your answer walter, could you expand on that? i tried to read about handle objects before posting the question, my understanding was that they cannot do what i am tryind to do. could you explain how you would do what i asked uding handle objects?

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

採用された回答

Walter Roberson
Walter Roberson 2020 年 8 月 27 日
classdef woman < handle
properties
children = handle([]);
end
methods
function obj = woman
end
function addchild(obj, this_child)
obj.children(end+1) = this_child;
end
end
end
classdef child < handle
properties
mother = handle([]);
end
methods
function obj = child(this_mother)
obj.mother = this_mother;
addchild(this_mother, obj);
end
end
end
  5 件のコメント
Nathan Bblanc
Nathan Bblanc 2020 年 8 月 28 日
thank both of you
Walter Roberson
Walter Roberson 2020 年 8 月 28 日
Yes, figuring out what actually want to do can be tough! And knowing when to stop designing can be hard!

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

その他の回答 (1 件)

James Tursa
James Tursa 2020 年 8 月 27 日
MATLAB does not have variable pointers ... at least not in the sense of C/C++ like you are probably alluding to.
  1 件のコメント
Nathan Bblanc
Nathan Bblanc 2020 年 8 月 27 日
how a about a different sense? is there a way to do what i am trying to do?

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

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by