Jump to content
  • Advertisement
Sign in to follow this  
willthiswork89

code orginization

This topic is 4593 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 read the tutoail on here and its by far the worst tutorial i have ever read can somebody explain them? like just normal functions no structs or classes keep it as basic as it gets....i really wanna know what to do and how to use them, along with gettting another cpp file into the mix because so far ive always used 1 MAIN.cpp and nothing else.

Share this post


Link to post
Share on other sites
Advertisement
Normally you declare functions in the header file and the functions go into the cpp file.

Inside myheader.h

bool FunctionOne( const char* param );
void FunctionTwo( int x, int y );
int FunctionThree();


Inside the my.cpp

#include "myheader.h"
bool FunctionOne( const char* param )
{
}
...


So in a third .cpp file you can #include "myheader.h" and use those functions

Now, as to how you add them to your project it depends on your environment ( ie have to add the files to your project, Makefile, Jamfile etc )

Share this post


Link to post
Share on other sites
okay so i have the declaring header file, i have one cpp to say what they do, then i have one to use them? why couldnt i just define what the functions do inside the header? like

//header.h

void blit_image(int x, int y, SDL_Surface* screen, SDL_Surface* apply)
{
//define what to do and actually blit the stuff in the header
}

then

main.cpp

header::blit_image(0,0,screen,background);

see what i mean?

Share this post


Link to post
Share on other sites
You want to minimize the amount of data you have in the header because of the compile times involved. Since there is a potential of having a .h file included into several hundred different files a change in the header will force a rebuild of all those.

Think of the header as something like a 'table of contents' or 'reference'. Headers should just contain the declarations of things and not implementations.

Share this post


Link to post
Share on other sites
then why would i wanna make them? it really isnt saving me a messy code at all... the main thing im trying to do is minimize the ammount of clutter, i want a file to hold all my info on building my sdl screen, then a file to call like BuildSDL::init(); so that really i have an initializer code and a file with the clutter in it, maybe a couple files with clutter.

Share this post


Link to post
Share on other sites
Generally, it is true that you want only function declarations (not implementations) in your header files, but there are sometimes exceptions. If you have a very short function (like an accessor function, for example) it is sometimes more convenient to just implement the function in the header. Most of the time I try to stay away from this approach though.

One other thing to consider...I may be wrong about this, but if you implement a function inside the class declaration, doesn't the compiler treat that as an inline function even if you don't use the "inline" keyword? I may not be remembering the details of this correctly, but I think I remember reading something about that.

If you are accidentally inlining a bunch of functions (especially big functions), that can cause serious code bloat.

Another reason to keep your declarations separate from your implementations is that it makes it easier to look at the code and know what's going on. Sometimes you just want to look at the function's declaration (to remember it's return type and argument list) without being bogged down by seeing the implementation too.

Share this post


Link to post
Share on other sites
You don't have to. You can place everything into the .cpp file if you want. As soon as you start breaking out stuff into multiple files you will see the advantages of properly seperating stuff into headers and source files.

Share this post


Link to post
Share on other sites
Quote:
Original post by willthiswork89
okay so i have the declaring header file, i have one cpp to say what they do, then i have one to use them? why couldnt i just define what the functions do inside the header? like

//header.h

void blit_image(int x, int y, SDL_Surface* screen, SDL_Surface* apply)
{
//define what to do and actually blit the stuff in the header
}

then

main.cpp

header::blit_image(0,0,screen,background);

see what i mean?


yes i do.

but that isnt the way most people would do it. the way you're doing it the compiler sees the final code as one massive source file. so it has to recompile everything if you change one thing. this takes much longer than selective compilation.

it will also cause problems if you need to use the function in two other source files. theis will cause 2 copies of the code to be generated, which confuses the linker.

perhaps an example will illustrate:


your way first:

main.o
{
has symbol:
int main( int, char*[] )
void blit_image(int, int, SDL_Surface*, SDL_Surface*)

needs symbol:
// none

}



this entire thing gets compiled every time

okay say we add another file that needs blit_surface, dependant.o


main.o
{
has symbol:
int main( int, char*[] )
void blit_image(int, int, SDL_Surface*, SDL_Surface*)


needs symbol:
//none
}

dependant.o
{
has symbol:
void some_function_that_needs_blitting()
void blit_image(int, int, SDL_Surface*, SDL_Surface*)

needs symbol:
//none
}



this will confuse the linker because it finds 2 versions of blit_image, that take the same arguments. it doesnt check if they are the same, it will just complain.

the ideal way:

main.o
{
has symbol:
int main( int, char*[] )

needs symbol:
void blit_image(int, int, SDL_Surface*, SDL_Surface*)
}

// blit.cpp gets compiled seperatly.
// blit.h contains the symbol names
blit.o
{
has symbol:
void blit_image(int, int, SDL_Surface*, SDL_Surface*)

needs symbol:
SDL_BlitSurface( SDL_Rect*,SDL_Surface*,SDL_Rect*,SDL_Surface*)
}

dependant.o
{
has symbol:
void some_function_that_needs_blitting()

needs symbol:
void blit_image(int, int, SDL_Surface*, SDL_Surface*)
}



so the linker, while in main.o says "i need a blit_image function that takes a pair of ints and a pair of SDL_Surface pointers." it looks around and finds an exact match in blit.o; its happy and your appllication can run.

does this help?

Share this post


Link to post
Share on other sites
i see that your seperating the things but where in the code are you actually defining the insides of the blit? lets just do somthing simple like

main.cpp

i want to get the number output of 2 * 2

i know i can get it easily througn main but for reason we will say we need to make a header to do this

would i do

multiply::outcome(2);

in main or would i do

multiply:: int outcome(int outcome)
{
outcome = outcome * 2;
return outcome;
}

im probably just confusing you but really what i wanna know is how could i use a header to find 2 * 2 and allow main.cpp use the function from the header to get the answer?
multiply.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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!