www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Method of another module is callable, even if they is private.

reply "Namespace" <rswhite4 googlemail.com> writes:
A.d:
----
import B;

class Foo {
public:
	void call(Bar b) {
		b.test(42);
	}
}

void main() {
	Bar b = new Bar();
	Foo f = new Foo();
	f.call(b);
}
----

B.d:
----
import std.stdio;

class Bar {
private:
	void test(int id) {
		writeln("Bar called with ", id);
	}
}
----
As expected:
Output: A.d(6): Error: class B.Bar member test is not accessible

But with this little change of A's Foo class:
----
class Foo {
public:
	void call(Bar b) {
		void delegate(int) dg = &b.test;
		dg(42);
	}
}
----

It works: Bar.bar with 42

Bug or feature? :P
Oct 12 2013
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 10/12/13, Namespace <rswhite4 googlemail.com> wrote:
 Bug or feature? :P
Has to be a bug. Reduced: ----- module a; import b; void main() { auto s = S(); auto f = &s.f; // no error f(); // no error auto sf = &S.sf; // error } ----- ----- module b; struct S { private void f() { } private static void sf() { } } -----
Oct 12 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
On Saturday, 12 October 2013 at 19:07:44 UTC, Andrej Mitrovic 
wrote:
 On 10/12/13, Namespace <rswhite4 googlemail.com> wrote:
 Bug or feature? :P
Has to be a bug. Reduced: ----- module a; import b; void main() { auto s = S(); auto f = &s.f; // no error f(); // no error auto sf = &S.sf; // error } ----- ----- module b; struct S { private void f() { } private static void sf() { } } -----
Ok, I've opened a bug report for this. Otherwise it would be a nice C++-friend like feature. :D Of course, a bit protection would be needed.
Oct 12 2013