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