Very large function...can I make it global?
I have a series of very large functions I would like to be able to use throughout my code. My main.cpp file is becoming very long so I would like to move these functions into separate files. How do I go about doing this? Should I use keyword static?
Thanks for any help
// in foo.cpp// this is called the function "definition"void GlobalFunction( int i ){ // do stuff}// in foo.h// this is called the function "declaration";// other files need to see this declaration to// be able to call itvoid GlobalFunction( int i );// in main.cpp#include "foo.h"int main( ){ // call your global function; this is possible // because you included "foo.h", and hence the // function declaration is visible at this scope GlobalFunction( 0 ); return 0;}
[EDIT: emeyex beat my by two seconds!]
No, don't use 'static' ;)
Simply copy the function prototype (the first line of the function, with the return type, name and arguments) and put it in a header file.
e.g.
Currently you have:
main.cpp:
void foo( int bar )
{
...
}
So create a new header, say "myFunctions.h", and put this in it:
void foo( int bar );
Now you can put your "foo" function in any CPP file that you like. And you can use it in any other CPP file that you like - as long as those other files have "#include "myFunctions.h"
No, don't use 'static' ;)
Simply copy the function prototype (the first line of the function, with the return type, name and arguments) and put it in a header file.
e.g.
Currently you have:
main.cpp:
void foo( int bar )
{
...
}
So create a new header, say "myFunctions.h", and put this in it:
void foo( int bar );
Now you can put your "foo" function in any CPP file that you like. And you can use it in any other CPP file that you like - as long as those other files have "#include "myFunctions.h"
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement