Pointer vs Ref

Ali Çehreli acehreli at yahoo.com
Mon Jun 9 17:17:25 UTC 2025


On 6/9/25 12:24 AM, confuzzled wrote:

 > Is it possible to accomplish the following using ref instead of
 > pointers?

Your example is a good case for a pointer. Was there a reason why a 
reference should be used?

Having said that, you can even drop the pointer and use a DataSource as-is:

struct Engine
{
     DataSource dataSource;
     // ...
}

However, that would be preferable only if DataSource did not have a 
static array but I assume you wrote int[3] just as an example. Otherwise:

struct DataSource
{
     int[] data;    // <-- Now a slice
}

That works because DataSource.data is a *reference* to your external data.

Ali



More information about the Digitalmars-d-learn mailing list