你如何在 C++ 中定义一个全局函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6874346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you define a global function in C++?
提问by Cory Klein
I would like a function that is not a member of a class and is accessible from any class.
我想要一个不是类成员并且可以从任何类访问的函数。
I assume I would have to #include
the header file where the function is declared, but I don't know where to define such a global function.
我假设我必须#include
在声明函数的头文件中,但我不知道在哪里定义这样的全局函数。
Are there good reasons against having such a function in the first place?
首先是否有充分的理由反对拥有这样的功能?
回答by Necrolis
you need a body (in a cpp
file):
你需要一个正文(在一个cpp
文件中):
int foo()
{
return 1;
}
and a definition/prototype in a header file, which will be included before any use of the function:
以及头文件中的定义/原型,它将在任何使用该函数之前包含:
#ifndef MY_FOO_HEADER_
#define MY_FOO_HEADER_
int foo();
#endif
then using it somewhere else:
然后在其他地方使用它:
#include foo.h
void do_some_work()
{
int bar = foo();
}
or use an inline function (doesn't guarantee it'll be inlined, but useful for small functions, like foo
):
或使用内联函数(不保证它会被内联,但对小函数有用,例如foo
):
#ifndef MY_FOO_HEADER_
#define MY_FOO_HEADER_
inline int foo()
{
return 1;
}
#endif
alternatively you can abuse the C-style header based functions (so this goes in a header, the static
forces it to exist in a single compilation unit only, you should avoid this however):
或者,您可以滥用基于 C 样式的头文件的函数(因此这在头文件中,static
强制它仅存在于单个编译单元中,但是您应该避免这种情况):
#ifndef MY_FOO_HEADER_
#define MY_FOO_HEADER_
static int foo()
{
return 1;
}
#endif
回答by Benjamin Bannier
What you are calling global functionis usually called a free functionand they are A Good Thing.
您所称的全局函数通常称为自由函数,它们是一件好事。
You would define it just like a class' member function, but outside of that class' scope.
您可以像定义类的成员函数一样定义它,但在该类的范围之外。
double squared(double x) {
return x*x;
}
Simple functions you can define with the inline
keyword in the header file, or just declare it there
你可以inline
在头文件中用关键字定义简单的函数,或者只是在那里声明它
double squared(double x);
and put the implementation (first example) into the *.cpp
file.
并将实现(第一个示例)放入*.cpp
文件中。
回答by Chad
In a header file:
在头文件中:
// someheader.h
#ifndef MY_GLOBAL_FUN
#define MY_GLOBAL_FUN
void my_global_fun();
#endif
In an implementation file:
在实现文件中:
#include "someheader.h"
void my_global_fun()
{
// ...
}
In other files that require that function:
在需要该功能的其他文件中:
#include "someheader.h"
void f()
{
my_global_fun();
}
Free functions like this are useful and there are not many arguments against using them. Depending on your use case, its likely appropriate to put these functions in a specific namespace
to avoid name collision with other libraries you may be using.
像这样的自由函数很有用,并且没有太多反对使用它们的论据。根据您的用例,将这些函数放在特定的位置可能是合适的,namespace
以避免与您可能正在使用的其他库发生名称冲突。
回答by MGZero
Think of main(). The function is kind of just...there. It's not within any class, struct or namespace. You just declare and give it a body. Of course, in the case of functions that are not main, it's best to put a prototype in a header and the define it in a .cpp file.
想想 main()。该功能有点......在那里。它不在任何类、结构或命名空间中。你只需声明并给它一个身体。当然,对于不是 main 的函数,最好将原型放在头文件中,然后在 .cpp 文件中定义它。
Remember, C did not have classes and structs could not hold member functions. There was nothing wrong with free functions then and there isn't now.
请记住,C 没有类,结构不能容纳成员函数。那时免费功能没有任何问题,现在也没有。
回答by iammilind
You have to declare its prototype in header file and define it in implementation file.
您必须在头文件中声明其原型并在实现文件中定义它。
//file.h
void foo();
//file.cpp
void foo ()
{}
To shortly answer your second question, Global functions are needed when they are used by several different classes and types in a generic way. For example math functions.
为了简短地回答您的第二个问题,当多个不同的类和类型以通用方式使用全局函数时,需要它们。例如数学函数。
Otherwise, in general you may avoid so many global functions. Also you should avoid having a static
local member or global data associated with such function (so that you don't have to worry about thread safety).
否则,通常您可能会避免使用这么多全局函数。此外,您应该避免使用static
与此类函数关联的本地成员或全局数据(这样您就不必担心线程安全)。
回答by Utkarsh Bhardwaj
In addition to the answer by @Necrolis, the use of static is deprecated in favour of unnamed namespaces. However, use of unnamed namespaces and static both creates separate copies for each translation unit which increases the size of binary. The use of inline is better than both of these in this sense.
除了@Necrolis 的回答之外,不推荐使用 static 以支持未命名的命名空间。然而,使用未命名的命名空间和静态都会为每个翻译单元创建单独的副本,这会增加二进制文件的大小。从这个意义上说,内联的使用比这两者都好。
These solutions allow for more usage specific optimisations by compilers but are less instruction cache friendly compared to definition in a source file and then linking.
这些解决方案允许编译器进行更多特定于使用的优化,但与源文件中的定义然后链接相比,指令缓存不那么友好。