简体   繁体   English

有什么方法可以将参数默认设置为先前参数的功能? [C ++]

[英]Is there any way to set parameter default as function of previous parameter? [C++]

I assume not, but I just wanted to check - is there any way in C++ to do something like the following? 我假设没有,但是我只是想检查一下-C ++中有什么方法可以执行以下操作? Obviously when I try the below I get a scope-based error about bar. 显然,当我尝试下面的内容时,我得到了关于bar的基于范围的错误。

void foo(Bar bar, int test = bar.testInt) { ... }

If there is a value of test that is invalid, you could detect that: 如果存在无效的test值,则可以检测到:

void foo(Bar bar, int test = -1) { //assuming -1 is invalid
    if(test == -1) test = bar.testInt;

    //...
}

If not, you could always use overloaded functions: 如果没有,则可以始终使用重载函数:

void foo(Bar bar, int test) {
    //...
}

void foo(Bar bar) {
    foo(bar, bar.testInt);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM