Class using static method causes clear warning
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Håkan Lundgren
le 21 Jan 2015
Commenté : Titus Edelhofer
le 26 Jan 2015
I am implementing a class (ClassA) that use a static method for the initialization of a help variable, needed at Construction. An instance of ClassA is then a property of another class, ClassB. This seems to provoke the following MATLAB warning:
"Warning: Objects of 'ClassA' class exist. Cannot clear this class or any of its superclasses."
The reason I use the static help method is that ClassA in its turn has a property (Property1) that is an Array of object of a third class, ClassC. I cannot seem to instantiate a 1-by-n ClassC at Construction of ClassA, but must instead separately construct the 1-by-n ClassC and return it from the static function in the definition of Property1.
The same thing happens when I create the variable used to initialize ClassA.Property1 using a "normal" function implemented as a utility external to ClassA. Construction and execution of all of this runs fine. It's just that I have the habit of doing >> clear all; close all; clear classes; between executions to make sure that I get a fresh start including all the changes in each run.
When I delete all uses of static funcions or external helper functions, the problem dissappears.
In the MATLAB documentation there is some brief mentioning of this error, but I cannot find anything about it explicitly related to class Construction using static methods.
3 commentaires
Guillaume
le 21 Jan 2015
In 2013b, you indeed need to clear classes (but only that) whenever you make edit to the class definition. As of 2014a, this is no longer necessary.
If you do get the warning and it is about one of your class, then in all likelihood the changes to your class have not been picked up. So you do have to worry about it.
I did happen the warning when I was writing classes in 2013b and prior. I don't think it's particularly to do with static methods. Most likely, it's the way references to other classes are being held.
Réponse acceptée
Titus Edelhofer
le 21 Jan 2015
Hi Hakan,
instead of a static method you could use a constant property. BTW, what version of MATLAB do you use? I remember those problems with not being able to clear classes was in previous versions of MATLAB but far less often in more recent versions ...
Titus
1 commentaire
Plus de réponses (1)
Håkan Lundgren
le 22 Jan 2015
1 commentaire
Titus Edelhofer
le 26 Jan 2015
Hi Hakan,
yes, that's right. When you have a constructor you always have to implement "default constructor", i.e., the constructor without arguments. Take the following example:
c(2) = myobj(input1, input2);
In this case MATLAB will call the constructor without input arguments to create the variable c(1).
Titus
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!