如何将可选参数传递给C ++中的方法? [英] How to pass optional arguments to a method in C++?

查看:131
本文介绍了如何将可选参数传递给C ++中的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将可选参数传递给C ++中的方法?
任何代码片段

How to pass optional arguments to a method in C++ ? Any code snippet...

推荐答案

以下是传递模式作为可选参数的示例

Here is an example of passing mode as optional parameter

void myfunc(int blah, int mode = 0)
{
 if(mode == 0)
  do_something();
 else
  do_something_else();
}

您可以用这两种方式调用myfunc并且都有效

you can call myfunc in both ways and both are valid

myfunc(10); //mode will be set to default 0
myfunc(10, 1);//mode will be set to 1

这篇关于如何将可选参数传递给C ++中的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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