CLion教程-修改签名

重构 | 修改签名
CtrlF6
修改签名重构将几种不同的修改应用于函数签名。您可以使用此重构来:
- 更改函数名称和返回类型
- 添加、删除和重新排序参数
在更改函数签名时,CLion 会搜索函数的所有用法,并更新可以安全修改以反映更改的函数的所有调用、实现和重写替换。
修改函数签名
- 将光标置于要重构的函数上。
- 按下 CtrlF6 或从主菜单或上下文菜单中选择“Refactor | Change Signature”。
在“Change Signature”对话框中,对函数签名进行必要的更改:
- 在“Name”字段中更改函数名称。
- 在“Return type”字段中更改函数返回类型。
- 使用表格和
/
/
/
按钮管理函数参数。
当添加参数时,在相应的行中指定新参数的属性:类型、名称和默认值。
“Default value”字段设置要用作参数传递给用法的类型值。如果将此字段保留为空,CLion 将使用默认的类型值(例如,数值的
0
和指针的nullptr
)。在“Type”和“Default value”字段中都可以使用代码补全。
- 您还可以使用相应的复选框将函数设置为
const
、constexpr
或noexcept
。
示例
C/C++
之前 | 之后 |
---|---|
//Initial function signature int exFunction2(int a, int b) { int x = a+b; return x; }; int main() { int a = 0, b = 1; int x = exFunction2(a, b); return 0; } | //Name changed, the third parameter introduced int exFunction3(int a, int b, int c) { int x = a+b; return x; }; int main() { int a = 0, b = 1; int x = exFunction3(a, b, 0); //default value '0' added return 0; } |
Objective-C/C++
之前 | 之后 |
---|---|
//This function will be turned into a block float multiply (int x, int y) { return x * y; } int main (int argc, const char * argv[]) { @autoreleasepool { float result; result = multiply( 10, 20 ); } return 0; } | int main (int argc, const char * argv[]) { @autoreleasepool { float result; result = //a block was generated ^float(int x, int y) { return x * y; }(10, 20); } return 0; } |
Python
之前 | 之后 |
---|---|
# This function will be renamed def fibonacci( n ): a, b = 0, 1 while b < n: print( b ) a, b = b, a+b n = int(input("n = ")) fibonacci( n ) | # Function with the new name def fibonacci_numbers( n ): a, b = 0, 1 while b < n: print( b ) a, b = b, a+b n = int(input("n = ")) fibonacci_numbers( n ) |
JavaScript
之前 | 之后 |
---|---|
//This function will be renamed //and a default parameter will be added function result() { } function show_result() { alert('Result: ' + result()); } | //Function with a new name and default parameter function generate_result(input = 100) { } function show_result() { alert('Result: ' + generate_result()); } |