Filter löschen
Filter löschen

Issue with delete(object)

25 Ansichten (letzte 30 Tage)
Aurélien
Aurélien am 21 Okt. 2022
Bearbeitet: Aurélien am 24 Okt. 2022
Hello,
I have a simple Matlab class. It has 2 properties, a string and an integer. The constructor is as follows:
function [obj, error] = constuctor_fct(arg1, arg2)
obj.property_str = arg1;
obj.property_int = arg2;
error = class.someOtherStaticMethod(obj.property_str, obj.property_int);
if (error ~= 0)
% There has been an issue, abort. Not relevant here
end
end
The issue I have is with the destructor. I want it to be this:
function destructor_fct(obj)
delete(obj)
end
However, when I call obj.destructor_fct() in my code, I get the following error:
Error using delete
Name must be a text scalar.
That seems very confusing to me, as I do not understand how a method used to delete objects should expect text scalars. Also, as far as I can tell and from what I tested, the static method called in the constructor does not interact with the object at all. I could just as well directly pass arg1 and arg2 to it, and the issue still happens.
Any idea what I did wrong ? Is delete not the correct method to delete an object from memory ? What should I use instead ?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Okt. 2022
delete() of objects applies to handle objects, not to value objects. You cannot delete a value object from inside the class.
  1 Kommentar
Aurélien
Aurélien am 24 Okt. 2022
Bearbeitet: Aurélien am 24 Okt. 2022
Indeed, I did not know there were different kinds of classes. It works properly when inheriting handle. Thanks !

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by