【发布时间】:2021-09-14 02:54:47
【问题描述】:
在python中我可以写:
def test(a, b=None):
if b is None:
return
else:
print(123)
在cpp中,最好避免使用指针,所以我改用引用,
那么如何做同样的事情呢?
#include "stdio.h"
void test(int a, const int &b) {
// how to check ?? since b should not be nullptr
printf("123\n"); };
int main() { test(); }
【问题讨论】:
-
引用永远不会为空。使用
std::optional表示可以为空的值。 -
您从哪里得知最好避免使用指针?
-
使用指针,而不是引用,仅此而已。
标签: python c++ default-arguments