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

发布于 2024-09-24 13:54:46 字数 37 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

巡山小妖精 2024-10-01 13:54:46

这是将模式作为可选参数传递的示例,

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

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

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

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(10);     // Mode will be set to default 0
myfunc(10, 1);  // Mode will be set to 1
も星光 2024-10-01 13:54:46

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

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

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

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 
極樂鬼 2024-10-01 13:54:46

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

myfile.h

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

myfile.cpp

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

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

myfile.h

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

myfile.cpp

void myfunc(int blah, int mode) /* mode = 0 */
{
    if (mode == 0)
        do_something();
     else
        do_something_else();
}
梦明 2024-10-01 13:54:46

你们中的一些人可能会感兴趣,在有多个默认参数的情况下:

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

给出以下函数调用:

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

产生以下输出:

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

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

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/

末骤雨初歇 2024-10-01 13:54:46

随着 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");
}

输出:

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

参见 https://en.cppreference.com/w/cpp/utility/可选

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

#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

—━☆沉默づ 2024-10-01 13:54:46

使用默认参数

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 = "" 

}

注意:以下格式不正确

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

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

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 )
誰ツ都不明白 2024-10-01 13:54:46

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

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

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
夏日浅笑〃 2024-10-01 13:54:46

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

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

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;
}
泪眸﹌ 2024-10-01 13:54:46

只需添加 @Pramendra 的接受的 ans ,如果您有函数的声明和定义,则仅在声明中需要指定默认参数

Jus adding to accepted ans of @Pramendra , If you have declaration and definition of function, only in declaration the default param need to be specified

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文