指向指针的默认值 [英] Default value for a reference to a pointer

查看:254
本文介绍了指向指针的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在函数中使最后一个参数成为默认参数,并且此参数的类型为*&(对指针的引用).由于某些原因,这对我不起作用:

I need to make last argument in my function to be a default argument and the type of this argument is *& (reference to pointer). For some reason this doesn't work for me:

template<class T>
void f(T*& = nullptr);  

我遇到错误:

错误1错误C2440:'默认参数':无法从'nullptr'转换为'T *&'

如何解决这个问题?

推荐答案

基本上,如果您需要使用nullptr调用此函数(这意味着我没有要传递给该函数的值,但无论如何都想调用它),那么您就想按T**接受参数.

Basically, if you need to call this function with nullptr (which means "I don't have a value to pass to the function, but want to call it anyway"), then you would want to take the argument per T**.

请参见 如何将对象传递给C ++中的函数? 更多关于传递参数的信息.

See How to pass objects to functions in C++? for more on passing arguments.

这篇关于指向指针的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆