pointers in object-oriented matlab programing

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 comentarios

Walter Roberson
Walter Roberson el 27 de Ag. de 2020
MATLAB objects derived from "handle" class effectively use pointers.
Nathan Blanc
Nathan Blanc el 27 de Ag. de 2020
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?

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Ag. de 2020
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 comentarios

You need to make the mother and children properties arrays of the correct type. I'd use the empty method.
children = child.empty(1, 0);
mother = woman.empty(1, 0);
Also addchild() probably also needs to update the child's mother property.
% Two women
>> w1 = woman;
>> w2 = woman;
% Two children, one for each mother
>> c1 = child(w1);
>> c2 = child(w2);
% Woman w1 now has two children, c1 and c2.
>> addchild(w1, c2);
>> numel(w1.children)
ans =
2
% But woman w1 is not child c2's mother
>> isequal(c2.mother, w1)
ans =
logical
0
% Woman w2 is still child c2's mother
>> isequal(c2.mother, w2)
ans =
logical
1
Either c2's mother property should change to list only w1 as c2's mother or it should change to list both w1 and w2 as c2's mother. In the latter case you probably also need a way to remove a child or mother from the appropriate property.
Walter Roberson
Walter Roberson el 27 de Ag. de 2020
In the interface I tossed together, addchild should probably be a private method with access granted specifically to child. You would never call it directly: when you call child() you have to pass in the mother information and the constructor for child invokes addchild on the mother.
You would certainly want to improve the interface: adoption, surrogate mothers, same-sex marriage, blended eggs, children of unknown mothers...
Steven Lord
Steven Lord el 27 de Ag. de 2020
All that is true, and those are good points for nathan blanc to consider when designing their real class hierarchy. Sometimes the process of designing a class hierarchy can be more difficult and/or time consuming than implementing it!
Nathan Blanc
Nathan Blanc el 28 de Ag. de 2020
thank both of you
Walter Roberson
Walter Roberson el 28 de Ag. de 2020
Yes, figuring out what actually want to do can be tough! And knowing when to stop designing can be hard!

Iniciar sesión para comentar.

Más respuestas (1)

James Tursa
James Tursa el 27 de Ag. de 2020

0 votos

MATLAB does not have variable pointers ... at least not in the sense of C/C++ like you are probably alluding to.

1 comentario

Nathan Blanc
Nathan Blanc el 27 de Ag. de 2020
how a about a different sense? is there a way to do what i am trying to do?

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Ag. de 2020

Comentada:

el 28 de Ag. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by