D style - member functions

DLearner bmqazwsx123 at gmail.com
Wed Apr 26 18:24:08 UTC 2023


Consider:
```
struct S1 {
    int A;
    int B;
    int foo() {
       return(A+B);
    }
}

struct S2 {
    int A;
    int B;
}
int fnAddS2(S2 X) {
    return (X.A + X.B);
}

void main() {
    import std.stdio : writeln;

    S1 Var1 = S1(1, 2);
    writeln("Total Var1 = ", Var1.foo());

    S2 Var2 = S2(1, 2);
    writeln("Total Var2 = ", fnAddS2(Var2));

    return;
}
```

Of the two ways shown of producing the total from the same 
underlying structure, which is the better style?

Further, do we care about the situation where there are many 
variables of type 'S', which presumably means the function code 
generated from S1 gets duplicated many times, but not so with S2?




More information about the Digitalmars-d-learn mailing list