Sign in to follow this  

Is there some problem to put large codes in a header?

This topic is 4690 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

Just a basic question: Is there some problem to put large codes in a header? For example: Suppose that I have a main program and a big mesh class/functions etc. Usually, we do something like this: mesh.h mesh.ccp main.cpp // mesh.h is called here Is there some problem to put mesh.cpp code into the mesh.h? So, we would have: mesh.h main.cpp Would not it be simpler? Thanks in advance

Share this post


Link to post
Share on other sites
Yes, it would be simpler, but not that efficient. Ultimetly, the contents of mesh.cpp mesh.h are pasted into the Main.cpp file - thus eliminating the need for that file at all. That's assuming that is the only .cpp file that uses it. If you had more .cpp files that needed to use that file, then you could not get it to compile with that method, because the functions would be redefined in every object file - thus causing you lots of headaches and linker errors.

Now you need the header file to reference the functions of the mesh class throughout the program without actually redefining them. It may seem tedious and useless, but it is actually better this way. Think about if you had a very large project. Would you rather have to place all the code in your main file or simply use a header file to use those function - think about "Windows.h". Do you kind of see now why using the header files is important? One last thing is that if you make a .dll or a static .lib from your file - you must use a .h file to refrence it, no pasting of the source to there.

- Drew

[edit] mesh.h is pasted into the main.cpp file, mesh.cpp does not exist

[Edited by - Drew_Benton on February 11, 2005 12:18:55 AM]

Share this post


Link to post
Share on other sites
Also if you want to change 1 line out of your mesh.cpp file then when its split you'll have to recompile mesh.cpp and then relink your app. If everything is in the mesh.h file then if you change 1 line out of that you'll also have to recompile every source file that includes mesh.h. If mesh.h is included in all/most of your other files then thats alot of recomiplation for only 1 minor change and on large projects would require you to wait hours, and even on smaller projects the extra 30 seconds can be a pain!

Share this post


Link to post
Share on other sites

This topic is 4690 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this