私の関数をコーディングして、参照へのポインタを適切に戻しています。 私は、機能がそれがやろうとしていたものを返すものの、std::cout
が結果を修正していたことがわかりました。 ここで何か間違っていますか? この動作を修正するにはどうすればよいですか? obj
は、関数呼び出しの生活のためにのみ有効であるため、std :: cout変数値を変更する
次のコードスニペットを参照してください、
#include "stdafx.h"
#include <iostream>
using namespace std;
class MyClass
{
public:
MyClass(int x_):m_Index(x_){}
int m_Index;
};
void myfunction(int *¤tIndex, MyClass obj)
{
currentIndex = &obj.m_Index;
}
int _tmain(int argc, _TCHAR* argv[])
{
MyClass obj(5);
int *Index = NULL;
myfunction(Index, obj);
int curr_Index = *Index;
cout << "Index = " << curr_Index << std::endl; // This works fine.
cout << "Index = " << *Index << std::endl; // This modifies *Index
return 0;
}
何coutで印刷され、そしてあなたはそれが印刷し何をしたいですか? – naffarn
"参照へのポインタを返す" - 'my関数は何も返しません。 int **へのポインタへの参照を**型とする引数をとります。 –