我想知道这两者有什么区别。我原以为他们俩的行为和陈述A是一样的
void myfunctReference(foo& f)
{
std::cout << "Function called";
}这里是来电者
报表A:
myfunctReference(foo()); //Fail - OK Agreed. Because a temp is being sent as a parameter to a function who parameter is not constant. temporaries can only bind to constant references报表B:
myfunctReference(*(new foo())); //Allowed - Why ? isnt *(new foo()) a temp ?发布于 2015-04-06 17:47:40
为什么?*(新的foo())不是临时的吗?
不,这不是“临时”。它是一个lvalue表达式,指的是新对象,这个对象是活着的,直到有人在它上调用delete。将非const值引用绑定到这样的表达式是非常好的。
这是一次记忆泄漏。
https://stackoverflow.com/questions/29476523
复制相似问题