Inheritance problem
Steven Schveighoffer
schveiguy at yahoo.com
Fri Feb 11 13:22:54 PST 2011
On Fri, 11 Feb 2011 16:14:31 -0500, %u <unknown at unknown.com> wrote:
> == Auszug aus Steven Schveighoffer (schveiguy at yahoo.com)'s Artikel
>
> Thanks, but what about the following:
>
> import std.stdio : writeln;
>
> class a {
>
> public this(int v) {
> myVar = v;
> }
>
> protected int myVar;
>
> }
>
> class b : a {
>
> private a[] moreInstances;
>
> this(int v, int[] vars...) {
> super(v);
> moreInstances ~= this;
>
> foreach(int cur; vars) {
> moreInstances ~= new a(cur);
> }
> }
>
> int getVar() {
> return moreInstances[1].myVar;
> }
>
> }
>
> void main(string[] args) {
> b exp = new b(0, 1, 2);
> writeln(exp.getVar());
> }
>
> This compiles fine and prints the number 1. myVar is also protected
> in class a, I also call myVar in the getVar()-method of class b.
Any code can access any members defined in the current module, regardless
of access attributes (that rule is outlined in the link I sent, I just
didn't quote that part). You have to split this into multiple modules to
see the other rules take effect.
-Steve
More information about the Digitalmars-d-learn
mailing list