Private does not prevent overriding, final prevents overriding. Private prevents the methods from being explicitly called from either user code or any derived classes.<br><br><div class="gmail_quote">On Wed, Aug 11, 2010 at 8:08 AM, Igor Lesik <span dir="ltr"><<a href="mailto:curoles@yahoo.com">curoles@yahoo.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">I think, it is not linker error at all.<br>
"private" in Transmogrifier prevents overriding; and since those 2 functions are<br>
not<br>
defined but still in use (vtbl) linker does not find them and complains.<br>
Replace "private" with protected (in CardboardBox also) and it all works.<br>
<br>
compiler should say that the functions are not overrided; I am sure a bug report<br>
already filed for this issue<br>
<br>
Igor<br>
<br>
From: Andrej Mitrovic<br>
Subject: Linker errors with Interfaces<br>
<div><div></div><div class="h5"><br>
Excerpt from TDPL, p213-214:<br>
<br>
interface Transmogrifier<br>
{<br>
// client interface<br>
final void thereAndBack()<br>
{<br>
transmogrify();<br>
untransmogrify();<br>
}<br>
<br>
// implementation interface<br>
private:<br>
void transmogrify();<br>
void untransmogrify();<br>
}<br>
<br>
class CardboardBox : Transmogrifier<br>
{<br>
override private void transmogrify()<br>
{<br>
// get in the box<br>
}<br>
<br>
override private void untransmogrify()<br>
{<br>
// get out of the box<br>
}<br>
}<br>
<br>
void play()<br>
{<br>
writeln("just playing");<br>
}<br>
<br>
void aDayInLife(Transmogrifier device, string mood)<br>
{<br>
if (mood == "play")<br>
{<br>
device.transmogrify();<br>
play();<br>
writeln(typeid(device));<br>
device.untransmogrify();<br>
}<br>
else if (mood == "experiment")<br>
{<br>
device.thereAndBack();<br>
}<br>
}<br>
<br>
import std.stdio;<br>
<br>
void main()<br>
{<br>
aDayInLife(new CardboardBox, "play");<br>
}<br>
<br>
<br>
Calling device.transmogrify() and device.untransmogrify() should not even be<br>
allowed to compile. I can comment these out, but the linker will still fail when<br>
<br>
there's a call to device.thereAndBack(); device is an object that inherits from<br>
interface Transmogrifier, and "thereAndBack()" is a public method of the<br>
interface, so I'm not sure whats wrong with that call.<br>
<br>
I get these in both cases:<br>
<br>
output\interface_test.obj(interface_test)<br>
Error 42: Symbol Undefined _D14interface_test14Transmogrifier12transmogrifyMFZv<br>
output\interface_test.obj(interface_test)<br>
Error 42: Symbol Undefined<br>
_D14interface_test14Transmogrifier14untransmogrifyMFZv<br>
<br>
<br>
One more thing, here's a class that inherits from the same interface, but tries<br>
to make the overriden methods non-private by mistake:<br>
interface Transmogrifier<br>
{<br>
<br>
// client interface<br>
final void thereAndBack()<br>
{<br>
transmogrify();<br>
untransmogrify();<br>
}<br>
<br>
// implementation interface<br>
private:<br>
void transmogrify();<br>
void untransmogrify();<br>
}<br>
<br>
class CardboardBox : Transmogrifier<br>
{<br>
override void transmogrify()<br>
{<br>
// get in the box<br>
}<br>
<br>
override void untransmogrify()<br>
{<br>
// get out of the box<br>
}<br>
}<br>
<br>
I get these errors:<br>
interface_test.d(23): Error: function interface_test.CardboardBox.transmogrify<br>
does not override any function<br>
interface_test.d(28): Error: function interface_test.CardboardBox.untransmogrify<br>
<br>
does not override any function<br>
<br>
The book has nice plausible error message alternatives:<br>
// Error: Cannot change protection of transmogrify from private to public<br>
// Error: Cannot change protection of untransmogrify from private to public<br>
<br>
I'd prefer if DMD put out those kinds of error messages in this case (or<br>
something similar).<br>
<br>
<br>
<br>
<br>
</div></div></blockquote></div><br>