小编asd*_*gry的帖子

如何在 C++ 中正确声明/分配变量的值

在赋值之前,我应该在 C++ 程序的顶部声明变量吗:

int i;
std::string x;
std::string retval;

x = "foo";
i = 5;
retval = somefunction();
Run Code Online (Sandbox Code Playgroud)

或者,通过以下方式为变量赋值是否正确/可接受:

int i = 5;
std::string x = "foo";
std::string retval = somefunction();
Run Code Online (Sandbox Code Playgroud)

我是 C++ 的新手,我想知道 C++ 社区接受哪种方式。

c++ variables declaration

1
推荐指数
1
解决办法
911
查看次数

函数参数的名称是否与函数调用中使用的变量相同?

说我有一个功能:

int add(int num1, int num2) { 
    num1 += num2;
    return num1;
}
Run Code Online (Sandbox Code Playgroud)

使用包含与函数参数名称相同的名称的变量调用函数在编程上是否正确?

例子:

int num1 = 10;
int num2 = 10;

int result = add(num1, num2) 
Run Code Online (Sandbox Code Playgroud)

或者为函数调用的变量/函数参数使用不同的名称在编程上是否正确。

c++ variables function

0
推荐指数
1
解决办法
66
查看次数

标签 统计

c++ ×2

variables ×2

declaration ×1

function ×1