This is a clean solution. And as a matter of fact, the compiler does NOT warn me while instantiate class with abstract method. This is different than Java, in which you must implement all abstract method while inherits from an abstract class.<br>
<br>Why I shall not instantiate a sub-class that has not fully implemented abstract methods of its parent class?<br><br>Thanks!<br>Shannon<br><br><br><div class="gmail_quote">2013/1/24 Michael Van Canneyt <span dir="ltr"><<a href="mailto:michael@freepascal.org" target="_blank">michael@freepascal.org</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5"><br>
<br>
On Thu, 24 Jan 2013, xrfang wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi All,<br>
I wrote a TPainter abstract class, and a TPaintRect class. In the TPaintRect class I have this code:<br>
<br>
procedure TPaintRect.OnMouseEnter(<u></u>Sender: TObject);<br>
var<br>
i: Integer;<br>
p: TPainter;<br>
begin<br>
for i := 0 to painters.Count - 1 do begin<br>
p := TPainter(painters.Objects[i]);<br>
try<br>
p.OnMouseEnter(Sender);<br>
except on EAbstractError do ; end;<br>
end;<br>
if Assigned(FOnMouseEnter) then FOnMouseEnter(Sender);<br>
end;<br>
<br>
While running in IDE, the program will crash because OnMouseEnter is abstract.<br>
<br>
My problems are:<br>
<br>
1) As I already wrapped it with try-except, I hope it won't trigger IDE exception. But even I turn off "Notify on Lazarus Exception" in debugger options it<br>
still pops up, and the popup said RunError(211), NOT EAbstractError. The program runs well outside of IDE.<br>
<br>
2) Is there a way to detect if an abstract method is implemented or not, without trying to call it and try...except?<br>
</blockquote>
<br></div></div>
The following will do it:<br>
<br>
uses sysutils;<br>
<br>
Type<br>
TParent = Class<br>
Procedure MyMethod; virtual; abstract;<br>
end;<br>
<br>
TChild = class(TParent)<br>
end;<br>
<br>
var<br>
C : TParent;<br>
<br>
begin<br>
c:=TChild.Create;<br>
If TMethod(@C.MyMethod).Code=<u></u>Pointer(@system.AbstractError) then<br>
Writeln('Not implemented')<br>
else<br>
Writeln('Implemented')<br>
end.<br>
<br>
When run, it will print 'Not implemented'.<br>
<br>
But you should not instantiate objects with abstract methods to begin with.<br>
<br>
The compiler warns you if you do.<br>
(compile the above program with warnings/hints to see it)<span class="HOEnZb"><font color="#888888"><br>
<br>
Michael.</font></span><br>--<br>
_______________________________________________<br>
Lazarus mailing list<br>
<a href="mailto:Lazarus@lists.lazarus.freepascal.org">Lazarus@lists.lazarus.freepascal.org</a><br>
<a href="http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus" target="_blank">http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus</a><br>
<br></blockquote></div><br>