[英]How to avoid redundant computation in constructors
我有一个像这样的构造函数:
C(T x) : base(f(x))
{
...
do something with f(x)
...
}
f(x)
没有公开为基类的成员。 如果无法修改C
的基数,如何避免两次计算f(x)
?
您可以使用两个构造函数,例如:
private C(WhateverFReturns x) : base(x)
{
//do something with x
}
public C(T x) : this(f(x))
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.