h. .cpp file coding
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: h. .cpp file coding

  1. #1
    Join Date
    Oct 2011
    Posts
    9

    h. .cpp file coding

    Hi All

    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?

    Thanks

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,594

    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.

  3. #3
    Join Date
    Oct 2011
    Posts
    9

    Thumbs up Re: h. .cpp file coding

    Quote Originally Posted by Eri523 View Post
    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.
    Thats great. Thank you for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center