Why `foo.x.saa.aa` and `foo.y.saa.aa` is the same? `shared_AA.saa` should still be instance variable, not class variable, right?

mw mingwu at gmail.com
Tue Jun 25 02:16:25 UTC 2024


Sorry about the silly code, but I just tried this:

```
$ cat shared_aa.d
import std;

synchronized class shared_AA_class {
  private:
   int[int] aa;
   alias aa this;

  public:
   void print() {
	  writeln(&aa, aa);
   }
}

struct shared_AA {
   shared_AA_class saa = new shared_AA_class();  // by this syntax 
`saa` is still instance variable?
   alias saa this;
}

class Foo {
	shared shared_AA x;
	shared shared_AA y;

	this() {
		x[1] = 1;  // only modified `x`, not `y`
	}
}

void main() {
	Foo foo = new Foo();
	foo.x.print();
	foo.y.print();
	writeln(&(foo.x.saa));
	writeln(&(foo.y.saa));
}
```

```
$ dmd ./shared_aa
$ ./shared_aa
63B699474020[1:1]
63B699474020[1:1]
76CDDB518010
76CDDB518018
```

```
$ ldc2 shared_aa.d
$ ./shared_aa
558A95DF90C0[1:1]
558A95DF90C0[1:1]
743BE2B00010
743BE2B00018
```

Why `foo.x.saa.aa` and `foo.y.saa.aa` is the same? (and of course 
print out the same contents).

`shared_AA.saa` should still be instance variable, not class 
variable, right?

Thanks.



More information about the Digitalmars-d-learn mailing list