Class using static method causes clear warning
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2015-1-21
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.
采纳的回答
Titus Edelhofer
2015-1-21
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 个)
Håkan Lundgren
2015-1-22
1 个评论
Titus Edelhofer
2015-1-26
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!