Returning an object of a second class through the

2019-09-05 05:39发布

I have a node class and a tree class. I have defined the node class to contain the properties needed for a node declaration and the tree class is used to form a tree structure from the nodes. While the tree structure is formed from the node, I am having a problem in returning the node object. My code structure is:

classdef Node
properties 
    node_center;
    node_size;
end
methods
   function this = Node(center,size)
       this.node_center = center;
       this.node_size = size;
   end
end
end % end of class Node
classdef Tree < handle
methods
   function n = Tree(points,objects_in_tree)
      n = Node(center_of_points,size);
      n = insert_child(n,center,sizez);
   end
end

Now the error I am getting is: When constructing an instance of class 'Tree', the constructor must preserve the class of the returned object. I know the reason of why its happening but would like to know the workaround to this. Thanks.

标签: oop matlab
1条回答
Emotional °昔
2楼-- · 2019-09-05 06:07

The return value from the constructor must be the object created - there's no way around it. You can make another function that returns the other values (like the Node) that you would like to get out of it. After the Tree is constructed, call the accessor function on that object.

查看更多
登录 后发表回答