临时对象/浪费对象可能是超低延迟应用程序的一大瓶颈。我试图让自己意识到不必要地调用构造函数的陷阱,所以我想知道是否有任何我不知道的方法。当构造函数被“静默”调用时,我知道以下方式:
1)
//a temporary "object" is created when adding x and y and then assigned to z
int x,y,z;
z = x + y;
2)
//A temporary object is created here when the return value is passed. Its also possible
//another temporary object is created during the assignment?
A a = my_function();
A my_function(){
return new A();
}
3)
A a = my_function();
A my_function(){
A a;
return a;
}
4) 参数按值传递的地方
A a;
my_function(a);
void my_function(A a){
}
5) 不使用初始化列表:
class A{
B b;
C c;
A(B bb, C cc):
{
this.b = bb;
this.c = cc;
}
}
还有其他隐式调用构造函数的例子吗?