Bypass the protection level

Namespace via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 11 04:40:50 PDT 2015


Let's say we have these files:

----
module Foo.Graphic.Drawable;

interface Drawable {
	void draw(bool);
}
----

----
module Foo.Graphic.Sprite;

import Foo.Graphic.Drawable;

class Sprite : Drawable {
protected:
	void draw(bool enable) {
		import core.stdc.stdio : printf;

		if (enable)
			printf("draw\n");
		else
			printf("no drawing...\n");
	}
}
----

----
module Foo.Window.Window;

import Foo.Graphic.Drawable;

class Window {
	void draw(Drawable d) {
		d.draw(true);
	}
}
----

I can call draw on Drawable, because it is declared public and I 
cannot call draw on Sprite because it is declared protected (this 
is already a bit weird, why can I redeclare the interface method 
draw as protected?) but I can call the protected draw method from 
Sprite through Drawable. Is this intended?


More information about the Digitalmars-d-learn mailing list