Is it a bug that a parent class that access its own private members from derived classes gets deprecation warning?

bauss jj_1337 at live.dk
Wed Apr 11 17:34:40 UTC 2018


On Wednesday, 11 April 2018 at 17:20:23 UTC, martin wrote:
> On Monday, 9 April 2018 at 17:16:56 UTC, bauss wrote:
>> On Monday, 9 April 2018 at 17:05:45 UTC, martin wrote:>>
>>> Actually, this behaves as i would expect.
>>> `_baz` is a private member of Foo (to be precise: it belongs 
>>> to module `a`)
>>> in handleBar(), you iterate `Bar[]` - which is in module `b`.
>>> By casting it to Foo, you are accessing the wanted module 
>>> (`a`) again.
>>
>> but handleBar() is in a.
>>
>> This behavior is allowed in ex. C#
>
> `_baz` is a member of `module a : Foo` - `_baz`, as is 
> `handleBar()`.
> `protected` is the Access specifier you want.
> If i understand you correctly, you want it to behave as  if 
> `_baz` would be a member of `handleBar()`
>
> If this is really possible in C#, it lets my eyebrow raise.

Execute the following C# program and it will work just fine:

It's a really common pattern with OOP.

public class Foo
{
	private string _baz;
	
	public void HandleBar(Bar bar)
	{
		bar._baz = "Hello";
	}
}

public class Bar : Foo
{
}

...

var bar = new Bar();
bar.HandleBar(bar);


More information about the Digitalmars-d mailing list