我有两种方法为指针创建实例。 但其中一人将失败。为其他作用域中的指针创建实例
class A {
public:
int num;
};
void testPointer1(A* a){
a = new A();
a->num = 10;
}
A* testPointer2(){
A* a = new A();
a->num = 10;
return a;
}
void testPointer() {
A* a1 = NULL;
testPointer1(a1); // this one fails
//cout << a1->num << endl; // segmentation fault
A* a2 = NULL;
a2 = testPointer2();
cout << a2->num << endl;
}
为什么testPointer1错了?
+1:我喜欢你分解它的方式来说明确切的问题 - 酷! – 2011-01-07 11:15:04