Passing a derived class where base class is defined as ref parameter

chopchop bullshiiii at gmail.com
Tue Dec 14 17:20:18 UTC 2021


On Monday, 13 December 2021 at 22:30:59 UTC, Adam D Ruppe wrote:
> On Monday, 13 December 2021 at 22:06:45 UTC, chopchop wrote:
>> If I remove the ref, it works as expected, that is to say I 
>> can give a derived class as parameter.
>
> Why are you using the ref to begin with?
>
>> What the logic here?
>
> Consider this:
>
>
> class C : A {}
>
> void incr(ref A a) {
>    a = new C;
> }
>
> B b = new B;
> incr(b);
> // oops b now got rebound to a C instead of to a B, which 
> breaks everything

Hi Adam,

I am using the "ref" here (I put tinyurl to avoid 
over-referencing the post instead of the github page itself):
https://tinyurl.com/bdddkmub

I would like to be able to pass any kind of console to 
updateFoodToken ( Console c ), ie either a winconsole or a 
nixconsole, which are derived from Console.

I mean I probably have a cognitive bias of being a c++ dev. Let 
me explain. If I was coding in C++ I would pass "A&". Not "const 
A&", because I do two function calls with console - for example 
console.gotoxy(...) - but those 2 members functions are not 
suffixed "const". Since it is work in progress, I dont even know 
if I am going to modify those functions at the end or do 
something else with console...


But well, what do you guys think? The documentation does not even 
say if an "in" param is passed by value or reference ("may be 
passed by reference" it says). I don't really see how a copy of a 
console would work... Kind of eery in my minde.


Adam, I am actually using your code :) You and anyone else are 
welcome to review / send some Pull Request, deal with the console 
code, comment, etc




More information about the Digitalmars-d-learn mailing list