ClassA* pa = NULL;
ClassA* pb = NULL;
assignObject(ClassA* pa,ClassB* pb)
{
pa = new ClassA;
pb = new ClassB;
}
what will be the value of pa,pb after executing the function
执行函数后pa,pb的值是多少
EDIT how to pass as pointer is the return if pa,pb is NULL
编辑如何传递如果指针是返回,如果pa,pb是NULL
4 个解决方案
#1
As pointed out in other answers - both will still be NULL after the call. However, there are two possible solutions to this problem:
正如其他答案所指出的那样 - 在通话后两者仍然是NULL。但是,这个问题有两种可能的解决方案:
1) references
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) pointers
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.
Most programmers would probably choose references because then they don't need to assert anything (references can never be NULL).
大多数程序员可能会选择引用,因为它们不需要断言任何东西(引用永远不能为NULL)。
#2
They will be NULL, since you're passing them by value. If you want to pass it by reference, you'd do this:
它们将为NULL,因为您按值传递它们。如果你想通过引用传递它,你会这样做:
ClassA* pa = NULL;
ClassA* pb = NULL;
assignObject(ClassA*& pa, ClassB*& pb)
{
pa = new ClassA;
pb = new ClassB;
}
Note, I'm not sure what you're trying to accomplish with the global variables. They're never used in this example, since the local variables (function parameters) hide them.
注意,我不确定你要用全局变量完成什么。它们从未在此示例中使用过,因为局部变量(函数参数)会隐藏它们。
I think you also need to declare a return value type for your function in order for it to be valid C++.
我认为您还需要声明函数的返回值类型,以使其成为有效的C ++。
#3
ClassA* pa = NULL;
ClassA* pb = NULL;
void assignObject(ClassA* &pa,ClassB* &pb)
{
pa = new ClassA;
pb = new ClassB;
}
Alternatively:
ClassA* pa = NULL;
ClassA* pb = NULL;
void assignObject(ClassA** pa,ClassB** pb)
{
*pa = new ClassA;
*pb = new ClassB;
}
#4
NULL, because you're passing the pointers in by value.
NULL,因为你按值传递指针。
#1
As pointed out in other answers - both will still be NULL after the call. However, there are two possible solutions to this problem:
正如其他答案所指出的那样 - 在通话后两者仍然是NULL。但是,这个问题有两种可能的解决方案:
1) references
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) pointers
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.
Most programmers would probably choose references because then they don't need to assert anything (references can never be NULL).
大多数程序员可能会选择引用,因为它们不需要断言任何东西(引用永远不能为NULL)。
#2
They will be NULL, since you're passing them by value. If you want to pass it by reference, you'd do this:
它们将为NULL,因为您按值传递它们。如果你想通过引用传递它,你会这样做:
ClassA* pa = NULL;
ClassA* pb = NULL;
assignObject(ClassA*& pa, ClassB*& pb)
{
pa = new ClassA;
pb = new ClassB;
}
Note, I'm not sure what you're trying to accomplish with the global variables. They're never used in this example, since the local variables (function parameters) hide them.
注意,我不确定你要用全局变量完成什么。它们从未在此示例中使用过,因为局部变量(函数参数)会隐藏它们。
I think you also need to declare a return value type for your function in order for it to be valid C++.
我认为您还需要声明函数的返回值类型,以使其成为有效的C ++。
#3
ClassA* pa = NULL;
ClassA* pb = NULL;
void assignObject(ClassA* &pa,ClassB* &pb)
{
pa = new ClassA;
pb = new ClassB;
}
Alternatively:
ClassA* pa = NULL;
ClassA* pb = NULL;
void assignObject(ClassA** pa,ClassB** pb)
{
*pa = new ClassA;
*pb = new ClassB;
}
#4
NULL, because you're passing the pointers in by value.
NULL,因为你按值传递指针。