将引用转换为 C++ 中的指针表示

2022-01-05 00:00:00 reference pointers c++

有没有办法在 C++ 中转换"对指针的引用?在下面的例子中,func2 已经定义了原型,我不能改变它,但是 func 是我的 API,我想要么传递两个参数,要么传递一个(第二个设置为 NULL)或两者都不设置(都设置为 NULL):

Is there a way to "convert" a reference to pointer in c++? In example below, func2 has already defined prototype and I can't change it, but func is my API, and I'd like to either pass both parameters, or one (and second set to NULL) or neither (both set to NULL):

void func2(some1 *p1, some2 *p2);

func(some1& obj, some2& obj2)
{
   func2(..);
}

推荐答案

func2(&obj, &obj2);

像普通变量一样使用引用参数.

Use reference parameters like normal variables.

相关文章