SomeOtherFunction
传递由值的指针,因此只有分配更改传递地址的本地副本。
为了使这项工作,通过引用传递指针:
void Main()
{
int* intPointer = NULL;
SomeFunction(intPointer);
//intPointer is still null?
}
void SomeFunction(int*& outInt)
{
SomeOtherFunction(outInt);
}
void SomeOtherFunction(int*& outInt)
{
outInt = new int(5);
}
说了这么多,是有什么错误使用返回值?
void Main()
{
int* intPointer = SomeFunction(intPointer);
//intPointer is still null?
}
int* SomeFunction()
{
return SomeOtherFunction();
}
int* SomeOtherFunction()
{
return new int(5);
}
[更新下面的评论。 ]
好吧,如果你有一个返回值,指示状态,想必指示整数是否已被阅读,那么你真正想要的是(使用bool
为您的具体情况的占位符):
void Main()
{
int intPointer = 0;
if (SomeFunction(intPointer) == true)
{
// read something
}
else
{
// failed to read.
}
}
bool SomeFunction(int& outInt)
{
return SomeOtherFunction(outInt);
}
bool SomeOtherFunction(int& outInt)
{
outInt = 5;
return true;
}
分配的内存我的返回值实际上就像ReceiveStatus(并且它会返回ReceiveStatus :: Successful)。所以我不能返回传输状态和同时收到的数据包。无论如何感谢那个男人! – Prodigga 2012-03-05 06:06:05