Executing the constructor of a handle object with or without semicolon

16 vues (au cours des 30 derniers jours)
Hi,
I'm seeing something that appears to me as very strange. I have handle class (called 'cube') which when I call the constructor like this:
a = cube(x,y,z,color,alpha);
Everything works as expected. However when I call the constructor methods without the semicolon, i.e.:
a = cube(x,y,z,color,alpha)
I find that the execution seems to continue pass the end of the constructor and run other functions in the methods section of my class. This is the class code...
classdef cube < handle
properties (GetAccess='public', SetAccess='private')
xIndex; % location of the cube in 3D space
yIndex;
zIndex;
end
properties (GetAccess='private', SetAccess='private')
graphicsHandle;
color;
alpha;
end
properties (Constant, GetAccess='private')
vertOffsets = [0 0 0; ... % cube vertices offsets configuration
1 0 0; ...
1 1 0; ...
0 1 0; ...
0 0 1; ...
1 0 1; ...
1 1 1; ...
0 1 1];
faces = [1 2 6 5; ... % cube faces configuration
2 3 7 6; ...
3 4 8 7; ...
1 4 8 5; ...
5 6 7 8; ...
1 2 3 4];
end
methods
function obj = cube(x, y, z, color, alpha) % constructor
obj.xIndex = x;
obj.yIndex = y;
obj.zIndex = z;
obj.graphicsHandle = [];
obj.color = color;
obj.alpha = alpha;
end;
function delete(obj) % destructor
if isempty(obj.graphicsHandle) == 0
delete(obj.graphicsHandle);
end
end;
function display(obj)
disp('displazing!');
dvert = obj.vertOffsets;
dvert(:,1) = dvert(:,1) + obj.xIndex; % translate vertices for drawing
dvert(:,2) = dvert(:,2) + obj.yIndex;
dvert(:,3) = dvert(:,3) + obj.zIndex;
% move the cube to the new location, creating the object if required
if isempty(obj.graphicsHandle)
obj.graphicsHandle = patch('Faces',obj.faces,'Vertices',dvert, ...
'FaceColor', obj.color, 'FaceAlpha', obj.alpha);
else
set(obj.graphicsHandle, 'Faces',obj.faces,'Vertices',dvert);
end;
end;
function move(obj, x, y, z)
obj.xIndex = x;
obj.yIndex = y;
obj.zIndex = z;
display(obj);
end;
end
end

Réponse acceptée

Philip Borghesani
Philip Borghesani le 17 Avr 2012
Your class has a display method which is called to display the variable "a" when the assignment is done without a semicolon.
If you enter a=5 without the semicolon the same is done for a with the value 5.
  1 commentaire
Michael Allwright
Michael Allwright le 17 Avr 2012
You were exactly right. Good spot, I had no idea I was overloading a special method. perhaps in the MATLAB editor there should be some sort of syntax highlighting when you override a method, as this can lead to a lot of confusion!

Connectez-vous pour commenter.

Plus de réponses (1)

Michael Allwright
Michael Allwright le 17 Avr 2012
For future reference, this is question is related to using 'display' as method name. This use of variable name caused the inbuilt display method to be overridden, and when not using a semicolon, would cause this method to execute as MATLAB echo'd or displayed the result of the assignment to the console.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by