如何将可选参数传递给 C++ 中的方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3784114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:43:17  来源:igfitidea点击:

How to pass optional arguments to a method in C++?

c++optional-arguments

提问by Swapnil Gupta

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

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

回答by Pramendra Gupta

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();
}

you can call myfunc in both ways and both are valid

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

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

回答by Alok Save

An important rule with respect to default parameter usage:
Default parameters should be specified at right most end, once you specify a default value parameter you cannot specify non default parameter again. ex:

关于默认参数使用的一条重要规则:
默认参数应该在最右端指定,一旦指定了默认值参数,就不能再次指定非默认参数。前任:

int DoSomething(int x, int y = 10, int z) -----------> Not Allowed

int DoSomething(int x, int z, int y = 10) -----------> Allowed 

回答by user2008151314

It might be interesting to some of you that in case of multiple default parameters:

有些人可能会感兴趣,如果有多个默认参数:

void printValues(int x=10, int y=20, int z=30)
{
    std::cout << "Values: " << x << " " << y << " " << z << '\n';
}

Given the following function calls:

鉴于以下函数调用:

printValues(1, 2, 3);
printValues(1, 2);
printValues(1);
printValues();

The following output is produced:

产生以下输出:

Values: 1 2 3
Values: 1 2 30
Values: 1 20 30
Values: 10 20 30

Reference: http://www.learncpp.com/cpp-tutorial/77-default-parameters/

参考:http: //www.learncpp.com/cpp-tutorial/77-default-parameters/

回答by Prasoon Saurav

Use default parameters

使用默认参数

template <typename T>
void func(T a, T b = T()) {

   std::cout << a << b;

}

int main()
{
    func(1,4); // a = 1, b = 4
    func(1);   // a = 1, b = 0

    std::string x = "Hello";
    std::string y = "World";

    func(x,y);  // a = "Hello", b ="World"
    func(x);    // a = "Hello", b = "" 

}

Note : The following are ill-formed

注意:以下格式不正确

template <typename T>
void func(T a = T(), T b )

template <typename T>
void func(T a, T b = a )

回答by Ben Voigt

With commas separating them, just like parameters without default values.

用逗号分隔它们,就像没有默认值的参数一样。

int func( int x = 0, int y = 0 );

func(); // doesn't pass optional parameters, defaults are used, x = 0 and y = 0

func(1, 2); // provides optional parameters, x = 1 and y = 2

回答by KenJ

To follow the example given here, but to clarify syntax with the use of header files, the function forward declaration contains the optional parameter default value.

为了遵循此处给出的示例,但为了使用头文件阐明语法,函数前向声明包含可选参数默认值。

myfile.h

我的文件.h

void myfunc(int blah, int mode = 0);

myfile.cpp

我的文件.cpp

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

回答by Jerry Coffin

Typically by setting a default value for a parameter:

通常通过为参数设置默认值:

int func(int a, int b = -1) { 
    std::cout << "a = " << a;
    if (b != -1)        
        std::cout << ", b = " << b;
    std::cout << "\n";
}

int main() { 
    func(1, 2);  // prints "a=1, b=2\n"
    func(3);     // prints "a=3\n"
    return 0;
}

回答by Fernando G. Testa

With the introduction of std::optional in C++17 you can pass optional arguments:

随着 C++17 中 std::optional 的引入,您可以传递可选参数:

#include <iostream>
#include <string>
#include <optional>

void myfunc(const std::string& id, const std::optional<std::string>& param = std::nullopt)
{
    std::cout << "id=" << id << ", param=";

    if (param)
        std::cout << *param << std::endl;
    else
        std::cout << "<parameter not set>" << std::endl;
}

int main() 
{
    myfunc("first");
    myfunc("second" , "something");
}

Output:

输出:

id=first param=<parameter not set>
id=second param=something

See https://en.cppreference.com/w/cpp/utility/optional

请参阅https://en.cppreference.com/w/cpp/utility/optional