开发者

How do you define a global function in C++?

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开发者_如何学Python don't know where to define such a global function.

Are there good reasons against having such a function in the first place?


you need a body (in a cpp file):

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):

#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):

#ifndef MY_FOO_HEADER_
#define MY_FOO_HEADER_
    static int foo()
    {
        return 1;
    }
#endif


What you are calling global function is usually called a free function and 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

double squared(double x);

and put the implementation (first example) into the *.cpp file.


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.


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.

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.


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).


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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜