阅读 4 个解释问题所在的答案相当烦人,但没有解释如何正确解决问题的方法。这可能是一个安全的猜测,如果 OP 不知道范围,他可能也不知道将变量传递给函数。
问题
您试图获取变量的值,但该变量在另一个函数中。我怎样才能得到它?嗯,简单的答案是,你不想得到它。你没听错。使用函数的全部原因是可重用性,如果你将新创建的函数绑定到另一个函数,那么你就不能在任何地方使用它。请记住,函数可以帮助你变得懒惰。一个好的程序员是一个懒惰的程序员。如果您可以编写一次函数并在百万个地方使用它,那么您就做对了。;)
但我仍然很想得到那个变量的值
然后您想使用函数参数将变量传递给函数。
函数被命名是因为你可以从数学的角度来思考它们。放入变量,在函数运行后取出有用的数据,并用这些变量做一些有趣的事情。所以假设你有一个数学函数y = f(x)
,这相当于int f(int x) { /*stuff here*/ }
你在你的主函数中调用它,使用int y = f(a)
wherea
是一些变量或数字。
您希望避免使用全局变量,因为它们并不总是符合您的预期(特别是如果您有很多代码,很容易不小心使用相同的名称。)
在您的情况下,您希望该函数打印出特定变量的内容,所以我认为您可能正在寻找一种方法将该函数与任何特定变量一起使用。所以这就是你如何做到的。
void f(); //hi, I'm a function prototype
void f(int a); //hi, I'm a function prototype that takes a parameter
void f(int a, int b); //hi, I'm a function prototype that takes two parameters (both ints)
void f(int a, ...); //hi, I'm a function prototype that takes an int then any number of extra parameters (this is how printf works.)
所以你真正想要做的是将你的代码更改为:
标头.h:
#ifndef HEADER_H
#define HEADER_H
#include <iostream>
// extern int* a; // We don't need this
void f(int* a)
{
if (a != NULL) //always, always check to make sure a pointer isn't null (segfaults aren't fun)
std::cout<<*a <<std::endl;
//return; //Don't really need this for a function declared void.
}
#endif
主.cpp:
#include "header.h"
int main()
{
int* a = new int(10);
f(a);
return 0; //main is declared as returning an int, so you should.
}
按值、指针和引用的函数
因此,在您给出的示例中,我使用了int
而不是int*
在您的示例中。两者的区别在于第一个是按值传递参数。另一个由指针。当您将变量传递给函数时,总是会制作它的副本。如果你传递一个int,它会复制一个int,如果你传递一个4 MB的结构,它将复制一个4MB的结构,如果你传递一个指向4MB结构的指针,它将复制一个指针(不是整个结构。)这很重要,原因有两个:
- 性能:制作 4MB 结构的副本需要一些时间。
- 更改内容的能力:如果复制指针,原始数据仍然在同一个地方,仍然可以通过指针访问。
如果你想要 1 而不是 2 怎么办?那么你可以声明指针const
。原型如下所示:int f(int const* a);
如果你想要 2 而不是 1 怎么办?坚韧的饼干(无论如何都没有充分的理由。)
最后,您还可以声明一个函数来获取引用而不是指针,引用和指针之间的最大区别是引用不会为 NULL(并且您不能在引用上使用指针算术。)您会想要通常使用按引用传递或按值传递。需要通过指针传递是我几乎不需要做的事情,根据我的经验,它更像是一种特殊情况。
通过引用传递:int f(int& a);
通过 const 引用传递:int f(int const& a);
所以总结一下:
if you have function that needs parameters:
then:
if you do not need to modify the contents:
then:
if the size of the variable is small:
pass by value: int f(int a);
else if the size of the variable is large:
then:
if the value of the address can be NULL:
pass by const pointer: int f(int const* a);
else:
pass by const reference: int f(int const& a);
else if you do need to modify the contents:
then:
if the value of the address can be NULL:
pass by pointer: int f(int* a);
else:
pass by reference: int f(int& a);
还有一些案例,但这些是主要案例,请参阅此网站了解更多详细信息。