0
我想知道這兩者之間有什麼不同。我期待他們兩人有相同的行爲語句中的將一個非常量傳遞給一個常量 - *(new foo())和foo()作爲參數傳遞時的差異
void myfunctReference(foo& f)
{
std::cout << "Function called";
}
這裏有來電
聲明答:
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 ?
'new foo()'返回一個指向指定對象的左值的臨時指針。 – 0x499602D2
new爲對象分配內存,除非你刪除它,否則會泄漏。所以不,這不是暫時的,但你不能這樣做。 – Dave
我知道我不應該這樣做,我只是出於好奇而問。 –