Ich codierte meine Funktion ist ordnungsgemäß einen Zeiger auf eine Referenz zurückgeben. Ich fand, dass, obwohl die Funktion zurückgab, was es tun sollte, std::cout
die Ergebnisse änderte. Mache ich hier etwas falsch? Wie behebt man dieses Verhalten?Std :: Cout Ändern des Variablenwerts
Bitte lesen Sie den folgenden Code-Schnipsel,
#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;
}
Was cout Druck ist, und was wollen Sie es drucken zu? – naffarn
"einen Zeiger auf eine Referenz" - "meine Funktion gibt nichts zurück. Es benötigt ein Argument, dessen Typ ** Verweis auf Zeiger auf int ** ist. –