The type you are trying to return to the caller is int*
. Using a typedef should help:
using IntPtr = int*;
void fun(IntPtr& ptr) // works
{
ptr = new int;
}
void fun(IntPtr* ptr) // works
{
*ptr = new int;
}
void fun(IntPtr ptr) // won't work
{
ptr = new int;
}
struct Foo
{
IntPtr ptr;
};
void fun(Foo& foo) // works
{
foo.ptr = new int;
}
void fun(Foo* foo) // works
{
foo->ptr = new int;
}
void fun(Foo foo) // won't work
{
foo.ptr = new int;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…