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

Stanislav Blinov stanislav.blinov at gmail.com
Tue Dec 14 17:27:13 UTC 2021


On Tuesday, 14 December 2021 at 17:20:18 UTC, chopchop wrote:

> 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...

Simple. Don't take a `ref`. Just take a `Console`. Classes in D 
are reference types, you're not making a copy as you would in C++ 
if you were to write `updateFoodToken(Console c)`.


More information about the Digitalmars-d-learn mailing list