【发布时间】:2013-06-26 20:11:29
【问题描述】:
在 C++ 中,有没有办法在初始化列表中包含临时变量之类的东西。我想用相同的实例初始化两个常量成员,而不必将其传入,删除 const 要求,使用工厂(即传入但让工厂生成它以对 API 用户隐藏它),或者让 temp 实际上是一个成员变量。
即像
Class Baz{
const Foo f;
const Bar b;
Baz(Paramaters p):temp(p),f(p,temp),b(p,temp){ //temp is an instance of Something
// But NOT A member of Baz
// Whatever
}
}
而不是
Class Baz{
Foo f;
Bar b;
Baz(Paramaters p){
Something temp(p);
f = Foo(p,temp)
b = Bar(p,temp)
}
}
或
Class Baz{
Foo f;
Bar b;
Baz(Paramaters p,Something s):f(p,s),b(p,s){
}
}
【问题讨论】:
标签: c++ initialization constants