OOP: Object gone out of scope!

조회 수: 4 (최근 30일)
Ahmed Hossam
Ahmed Hossam 2017년 5월 12일
댓글: Ahmed Hossam 2017년 5월 12일
Hi,
I have two handle classes.
In one of them I call the constructor of the other handle class many times.
Problem: Upon entering the constructor of the other class, the object of the first class is gone and I can't access it's properties unless I define an instance of the first class...
Must I pass the object of the first class to the other class or is there another way of doing this?
Class1
def Class1 < handle
properties (Constant)
f = 1:2:20
g = 1:3:30
end
properties
a_Class1
b_Class1
c_Class1
end
methods
function obj = Class1(a,b,c)
obj.a_Class1 = a;
obj.b_Class1 = b;
obj.c_Class1 = c;
for k = 1:10
x(k) = Class2(k,f(k),g(k))
end
end
end
end
The values for a,b and c are user-specific values...
I need to access the values a,b and c in Class_2 without having to pass the whole object of Class1 to Class2...
The properties a_Class1, b_Class1 and c_Class1 are the same for both classes ...
Thank you for your advice!

채택된 답변

Sebastian Castro
Sebastian Castro 2017년 5월 12일
This is expected. x is a local variable to the constructor, so it will go out of scope after instantiating the object.
You have two options:
  • Declare x as a property of Class1
  • Use functions such as assignin or evalin to stick x into the MATLAB base workspace (or the function where the Class1 object is instantiated)
I'd strongly recommend going with the first option.
- Sebastian
  댓글 수: 1
Ahmed Hossam
Ahmed Hossam 2017년 5월 12일
Before reading this answer, I actually declared the a,b and c as properties of the other class. I think that's the best thing to do for now.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Software Development Tools에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by