C++ 中的默认参数
默认参数
C++ 中的默认参数是在声明函数时赋给函数参数的值。如果调用者在调用函数时未提供任何参数,则会自动赋值。如果调用者在函数调用期间提供了参数值,则会覆盖默认值。
语法
return_type function_name(param1 = v1, param2 = v2, ...);
此处
- return_type 定义返回值的类型,例如 int、float、void。
- function_name 是用户定义的函数名称。
- param1 和 param2 是函数参数的名称。
- v1 和 v2 是分配给这些参数的默认值。
默认参数规则
在 C++ 中定义默认参数时,应遵循以下关键规则,以确保代码高效且无错误。
1. 默认参数必须在函数声明中指定,而不是定义中。
第一条规则规定,默认参数必须在函数声明(原型)中指定,而不是在函数定义(实现)中指定。
如果在定义中指定了默认参数,或者在声明和定义中都指定了默认参数,则会引发编译错误。
示例
// 带有默认参数的函数声明 void greet(string name = "TutorialsPoint"); // 不带默认参数的函数定义(正确) void greet(string name) { cout << message << ", " << name << "!" << endl; }
2. 默认参数必须从右到左赋值
赋值默认参数时,必须从最右边个参数开始,到最左边个参数,并且用户不能跳过中间的参数赋值。
示例
// 有效 void funcA(int x, string y = "TutorialsPoint"); // 有效 void funcB(int x = 2006, string y = "TutorialsPoint" ); // 无效,因为默认参数必须从右到左赋值 void funcC(int x = 2006, int y);
3.默认参数不可更改或修改
一旦在函数声明中指定了默认参数,则在任何其他声明或定义中都不可更改或修改该参数。否则将抛出错误。
示例
void func(int x = 10); // 第一个声明 void func(int x = 20); // 第二个声明,将导致错误
4. 函数重载的歧义
从重载函数中调用默认参数可能会导致歧义,如果提供的参数不唯一,编译器将难以确定要调用哪个版本的函数。因此,请确保具有默认参数的函数在重载中不会相互冲突。
示例
void get(string name = "TutorialsPoint"); // 函数 1 void get(string name = "TutorialsPoint", int num = 2016); // 函数 2 get(); // 编译器应该选择哪个函数?
默认参数示例
这是一个简单的例子,展示了 C++ 中的默认参数。
#include <iostream> using namespace std; int main() { double price, discount = 12.0; // 默认折扣 cout << "Enter the price of the item: "; cin >> price; double total = price - (price * discount / 100); cout << "The total price after a " << discount << "% discount is: " << total << endl; return 0; }
当编译并执行上述代码时,它会产生以下结果 -
Enter the price of the item: 120 The total price after a 12% discount is: 105.6
表达式形式的默认参数
定义函数时,用户可以指定参数的默认值,这些默认值可以是常量值,也可以是表达式(包含变量、常量或函数调用),并在函数调用时进行求值。
示例
以下是默认函数参数的示例。
#include <iostream> using namespace std; double total_price(double price, double discount = 12.0) { return price - (price * discount / 100); } int main() { double price; cout << "Enter the price of the item: "; cin >> price; // 默认以 12% 的折扣计算总价 double total = total_price(price); cout << "The total price after a 12% discount is: " << total << endl; return 0; }
当编译并执行上述代码时,它会产生以下结果 -
Enter the price of the item: 120 The total price after a 12% discount is: 105.6
函数调用中的默认参数
调用函数来确定默认值称为函数调用中的默认参数。这会根据函数的逻辑或其他参数动态计算默认值。
#include <iostream> #include <cmath> // for value of using namespace std; double getDefaultWidth() { return 5.0; } double getDefaultRadius() { return 3.0; } double cuboid_vol(double l, double h, double w = getDefaultWidth()) { return l * w * h; } double cylinder_vol(double h, double r = getDefaultRadius()) { return M_PI * r * r * h; } int main() { double length = 10.0; double height = 7.0; // 具有默认宽度的长方体的体积 cout << "Cuboid volume : " << cuboid_vol(length, height) << " cubic units" << endl; // 自定义宽度的长方体的体积 cout << "Cuboid volume (custom width): " << cuboid_vol(length, height, 4.0) << " cubic units" << endl; // 具有默认半径的圆柱体的体积 cout << "Cylinder volume (default radius): " << cylinder_vol(height) << " cubic units" << endl; // 具有自定义半径的圆柱体的体积 cout << "Cylinder volume (custom radius): " << cylinder_vol(height, 6.0) << " cubic units" << endl; return 0; }
当编译并执行上述代码时,它会产生以下结果 -
Cuboid volume : 350 cubic units Cuboid volume (custom width): 280 cubic units Cylinder volume (default radius): 197.92 cubic units Cylinder volume (custom radius): 791.681 cubic units
默认参数的优势
- 它通过减少重载的数量来简化函数调用,当一个函数可以通过使用参数的默认值来处理不同的情况时,用户无需创建多个重载函数。
- 默认参数有助于避免代码重复并减少冗余。
- 它提高了代码的可读性并增加了灵活性,这使得将来的修改更加容易。