c++ - Is there any way to avoid code replication across different constructors of a class? -


is there way avoid code replication across different constructors of class?

class sample {     int a, b;     char *c; public:     sample(int q) :       a(0),       b(0),        c(new char [10 * q])     {     }      sample() :       a(0),       b(0),       c(new char [10])     {     } } 

it's called delegating constructor. in case this:

sample(int q) : sample(q, 10 * q) { }  sample() : sample(0, 10) { }  sample(int q, int d) : a(q),    b(q),     c(new char [d]) { } 

Comments