在C++中设置默认参数

Setting Default Parameters in C++

本文关键字:默认 参数 设置 C++      更新时间:2023-10-16

我对如何在C++中为函数参数提供默认值有一个小问题。我面临的问题可能是由于我对应该在函数原型或函数头中声明/定义参数的位置或两者兼而有之缺乏理解?代码如下,并注明了错误:

#include <iostream>
using namespace std;
float volume(float l, float w, float h);
int main() {
    float length;
    float width;
    float height;
    cout << volume() << endl; // Here, with 'volume()' underlined, it says:
    //no matching function for call to 'volume()'
    cout << "Length: ";
    cin >> length;
    cout << "Width: ";
    cin >> width;
    cout << "Height: ";
    cin >> height;
    cout << "Volume = " << volume(length, width, height) << endl;

}
float volume(float l = 1, float w = 1, float h = 1){
        float vol = l * w * h;
    return vol;
}

在另一次尝试中,发生的事情如下:

#include <iostream>
using namespace std;
float volume(float l = 1, float w = 1, float h = 1);
int main() {
    float length;
    float width;
    float height;
    cout << volume() << endl;
    cout << "Length: ";
    cin >> length;
    cout << "Width: ";
    cin >> width;
    cout << "Height: ";
    cin >> height;
    cout << "Volume = " << volume(length, width, height) << endl;

}
float volume(float l = 1, float w = 1, float h = 1){ //Here, Xcode says that
// that the error is: Redefinition of default argument. < which I believe I understand.
        float vol = l * w * h;
    return vol;
}

在我最后一次尝试中,这是有效的尝试,我做了这个:

#include <iostream>
using namespace std;
float volume(float l = 1, float w = 1, float h = 1);
int main() {
    float length;
    float width;
    float height;
    cout << volume() << endl;
    cout << "Length: ";
    cin >> length;
    cout << "Width: ";
    cin >> width;
    cout << "Height: ";
    cin >> height;
    cout << "Volume = " << volume(length, width, height) << endl;

}
float volume(float l, float w, float h){
        float vol = l * w * h;
    return vol;
}

有人可以向我解释为什么后者有效而前两个无效背后的逻辑吗?有没有另一种方法可以让代码仍然以与其他地方指定的参数或其他地方设置的默认值相同的方式工作?在这方面是否有任何公约或更优惠的做法?

亚当

C++和C是自上而下的解析。当编译器解释语句时,它不知道它尚未读取的内容。

在第一个示例中,您声明了一个名为"volume"的函数,原型化为获取 3 个浮点数并返回一个浮点数。然后,您尝试调用一个名为"volume"的函数,该函数不带任何参数,该函数尚不存在(它将是一个不同的函数,因为C++支持多态性)。稍后你定义了一个函数,它可以接受 0、1、2 或 3 个浮点数,但它都为时已晚,并且与第一个的原型不兼容。

你的第二个例子直观地是错误的,有点像定义变量两次,但我没有任何具体信息说明为什么当默认值相同时它是无效代码。

必须在函数原型中指定默认参数,该参数必须在首次使用之前发生,以便编译器了解它。通常,您会将原型及其默认值放在代码上方的头文件中。

处理共享头文件中的默认参数时需要注意的一件事,尤其是在将其与动态库一起使用时:参数的默认值存储在调用方而不是被调用的函数中。也就是说,如果使用新的默认值更新函数,并且不重新生成调用该函数的代码,则调用代码仍将使用旧的默认值。

Bjarne Stroustrup对上述答案的补充

默认参数在函数声明时进行类型检查,并在调用时计算。只能为尾随参数提供默认参数。例如:

int f (int, int =0 , char * =0 );//ok

int g (int =0 ,

int =0 , char *);//error

int h (int =0 ,

int , char * =0 );//error

默认参数可以在同一范围内的后续声明中重复,但不能更改。例如:

空 f (整数 x = 7 );

void f (int = 7 );//ok

void f (int = 8 );//error: 不同的默认参数

void g () { void f (int x = 9 );//ok: 此声明隐藏了外部声明 }

在嵌套作用域中声明名称

以使该名称在外部作用域中隐藏相同名称的声明容易出错。

默认值可以在函数声明中定义,就像您在第三次尝试中所做的那样。这意味着它们通常出现在头文件中,尽管这不是规则。

请注意,函数声明是有作用域的。这意味着一个函数可以有多个声明,只要它们具有不同的作用域:

void f(int);
int main() {
    f(3); // argument should specified.
    void f(int = 1);
    f(); // calls f(1)
}
void f(int n = 2) {
}
void g() {
    f(); // calls f(2)
}

在第二次尝试中,您将默认值放在函数的声明和定义上。这会导致编译器混淆,因为它们位于同一范围内。