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

kdevel kdevel at vogtner.de
Sun Apr 8 14:45:34 UTC 2018


On Sunday, 8 April 2018 at 13:00:02 UTC, bauss wrote:

[...]

> // a.d
> module a;
>
> class Foo
> {
> 	private:
> 	bool _baz;
> 	
> 	public:
> 	void handleBar(T : Foo)(T[] foos)
> 	{
> 		foreach (child; foos)
> 		{
> 			child._baz = true; // Not ok.

replace this line with

    import std.stdio;
    __PRETTY_FUNCTION__.writeln;
    foos.writeln;

This writelns:

    void a.Foo.handleBar!(Bar).handleBar(Bar[] foos)
    [b.Bar, b.Bar]

So foos is actually an array of Bars. And there is no access from 
a Bar to the private elements of it's baseclass Foo.

What I am wondering about is why does the method template match 
in the first place?

> 			(cast(Foo)child)._baz = true; // Ok.

You also may use the ternary conditional operator for that (SCNR).



More information about the Digitalmars-d mailing list