有什么方法可以避免类的不同构造函数之间的代码复制?
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])
{
}
}
最佳答案
它被称为委托(delegate)构造函数。在您的情况下,它看起来像这样:
sample(int q) : sample(q, 10 * q)
{
}
sample() : sample(0, 10)
{
}
sample(int q, int d) : a(q),
b(q),
c(new char [d])
{
}
关于c++ - 有什么方法可以避免跨类的不同构造函数进行代码复制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38634427/