有关指针参数的问题
#include "stdafx.h"
#include <iostream>
using namespace std;
class Test{
public:
int x;
int y;
};
void init(Test *t)
{
t = new Test;
t->x = 1;
t->y = 2;
}
int _tmain(int argc, _TCHAR* argv[])
{
Test *a = NULL;
init(a);
cout<<a->x<<endl;
return 0;
}
这样初始化指针为什么会报错呢
[解决办法]
t是个局部变量,a只是把自己的值传递给t而已,new到的给了t而没有给a,所以a还是个空指针