mercredi 23 janvier 2019

When delegating constructors, is it better to chain or have every constructor call one generic constructor?

I have a class with many possible combinations of parameters in the constructor that prevents simply listing all of them as default parameters. I can either chain constructor calls like this:

class A{
    A(obj a, obj b, obj c){/*code*/}
    A(obj b, obj c):A(defaulta, b, c){}
    A(obj c):A(defaultb, c){}
}

Or I can call the deepest constructor every time like this:

class B{
    B(obj a, obj b, obj c){/*code*/}
    B(obj b, obj c):B(defaulta, b, c){}
    B(obj c):B(defaulta, defaultb, c){}
}

Is there an advantage to doing one over the other?

Since the body of the delegating constructors is empty, does it change the compiled program or affect runtime performance, and if it does, can the compiler optimize it to the better option?

Aucun commentaire:

Enregistrer un commentaire