void function(typeA* ptr_to_A) {
if (!ptr_to_A) {
typeB B; // typeB is a derived class of typeA
ptr_to_A = &B;
}
do_stuff_to_ptr_to_A(ptr_to_A);
// my hope is that B is still in scope here so that this function will operate on my B object (which is on the stack) which only gets created if ptr_to_A was initially NULL
}
这个函数会做我认为它做的事情(我想要它做的事情)吗?也就是说,如果参数是空指针,只在堆栈上分配 B ?