正如其他答案所指出的那样 - 在通话后两者仍然是NULL.但是,这个问题有两种可能的解决方案:
1)参考
void assignObject(ClassA*& pa, ClassB*& pb) { pa = new ClassA; pb = new ClassB; } ClassA* pa = NULL; ClassA* pb = NULL; assignObject(pa, pb); // both will be assigned as expected.
2)指针
void assignObject(ClassA** pa, ClassB** pb) { assert(pa != NULL); assert(pb != NULL); *pa = new ClassA; *pb = new ClassB; } ClassA* pa = NULL; ClassA* pb = NULL; assignObject(&pa, &pb); // both will be assigned as expected.
大多数程序员可能会选择引用,因为它们不需要断言任何东西(引用永远不能为NULL).
正如其他答案所指出的那样 - 在通话后两者仍然是NULL.但是,这个问题有两种可能的解决方案:
1)参考
void assignObject(ClassA*& pa, ClassB*& pb) { pa = new ClassA; pb = new ClassB; } ClassA* pa = NULL; ClassA* pb = NULL; assignObject(pa, pb); // both will be assigned as expected.
2)指针
void assignObject(ClassA** pa, ClassB** pb) { assert(pa != NULL); assert(pb != NULL); *pa = new ClassA; *pb = new ClassB; } ClassA* pa = NULL; ClassA* pb = NULL; assignObject(&pa, &pb); // both will be assigned as expected.
大多数程序员可能会选择引用,因为它们不需要断言任何东西(引用永远不能为NULL).