Jump to content
  • Advertisement
Sign in to follow this  
saeedm

Very large function...can I make it global?

This topic is 3937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement


// 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 it
void 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;
}



Share this post


Link to post
Share on other sites
[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"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!