October 21st, 2011, 05:01 AM
h. .cpp file coding
I understand the concept of having a header file for all my function prototypes etc... with all the main body of the code is to be placed in the .cpp file.
However, in Visual C++ Forms, I have my Form which is a .h file. So does the same rule apply here, I mean When I double click a button on my form it automaitcally creates an
event handler in the .h Form for my button and I presume it wants me to write the bulk of my code in the .h form otherwise it would surely create the event handlers in the .cpp file (of which i dont seem to have one for my form!?).
so I guess my question is that, is there an exception to the rule of having the interfaces in the .h file and the code writing in the .cpp file doesnt apply to VS C++ Forms?
October 21st, 2011, 07:05 AM
Re: h. .cpp file coding
Basically, the rule of spearating declarations and implementations into .h and .cpp files applies to C++/CLI as well, it's just the Windows Forms Designer that doesn't care much about that. As you correctly observed, it doesn't even create a .cpp file for the Form1 that initially gets created for a new Windows Forms project. You'll notice, however, that it will create .cpp files for further forms you add to the project, but then later ignore them and still place event handler implementations in the .h file. (However, even if they're almost empty, these .cpp files aren't useless: Their purpose is to be compiled as part of the project and include the .h file that otherwise wouldn't get compiled at all.)
Keeping up the separation of declarations and implementations demands some manual work as described in http://www.codeguru.com/forum/showthread.php?p=2032109 but that will become familiar after some time and won't be really tideous anymore then. The topic has been discussed around here some times and http://www.codeguru.com/forum/showthread.php?p=2032707 (that originally is about a different topic, however) has attached a tiny sample project that may demonstrate what proper separation looks like.
Note that separating implementations from declarations not just is good style, it also has substantial benefits: For instance, it is a probate way of fighting the quite common circular inclusion problem (many discussions about which can be found around here using a forum search). In larger projects it may also notably reduce build time.
Last edited by Eri523; October 21st, 2011 at 07:08 AM.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
October 21st, 2011, 07:39 AM
Re: h. .cpp file coding
Thats great. Thank you for your help.
Originally Posted by Eri523
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!