Passing Structs to function like in C

D.Rex via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 14 07:52:53 PDT 2016


On Saturday, 13 August 2016 at 18:37:54 UTC, Cauterite wrote:
> On Saturday, 13 August 2016 at 15:47:51 UTC, D.Rex wrote:
>> /* memory.d file */
>> module memory;
>> import include.linux.sched;    /* contains task_struct 
>> definition */
>>
>> void free_page_tables(task_struct* tsk) {
>>         /* do something with &tsk */
>> }
>>
>> And to use the method from somewhere else
>> /* use method.d */
>>
>> module usemethod;
>> import include.linux.sched;
>> import mm.memory;
>>
>> void some method() {
>>         free_page_tables(*pointer to task_struct*);
>> }
>>
>> I hope my explanation is not rambling nonsense.
>>
>> Cheers.
>
> Yes, you're right. Passing structure pointers to functions is 
> an extremely common practice in C, because there aren't really 
> any other compelling options. In D we have things like methods, 
> classes, 'const ref' params, return-type inference, etc., so 
> there's usually a better way to achieve the same result.

Cheers!!

Speaking of classes, and this may have been answered elsewhere, 
but I am yet to find said answer, or am just missing something 
right in front of my face...but how does one go about accessing a 
method from a class if said class is passed to a function as a 
pointer?  By that I mean something like

class Foo {
     void fooMethod() {

}

void bar(Foo *f) {

}


More information about the Digitalmars-d-learn mailing list