C++/CLI Best Practices: .h versus .cpp files  
Author Message
Joe Hatfield





PostPosted: Visual C++ General, C++/CLI Best Practices: .h versus .cpp files Top

Hi,

I find it mildly unsettling that Visual Studio 2005 seems to put much if not all code for a generated C++/CLI class into the .h file.

Shouldn't most of the actual code for a class be written to the .cpp file with the .h file being left for declarations alone

Is a new convention forming that suggests otherwise

For you folks already programming in C++/CLI, how do you decide what code goes into what files

Thanks...Joe




Visual C++5  
 
 
Mike Danes





PostPosted: Visual C++ General, C++/CLI Best Practices: .h versus .cpp files Top

"Is a new convention forming that suggests otherwise "

No. C++/CLI works much the same as standard C++. It's just the designer that places code in the header file and this is most probable caused by a designer limitation (think that C++ is the only language that has this header/source file distinction, the other Visual Studio supported languages like C#, VB and J# are "single file").

I let the designer do its way but for the rest I stick to the usual way of doing things in standard C++, declarations in header, definitions in cpp file.


 
 
duck thing





PostPosted: Visual C++ General, C++/CLI Best Practices: .h versus .cpp files Top

I let the designer do its way but for the rest I stick to the usual way of doing things in standard C++, declarations in header, definitions in cpp file.

Seconding this approach. The compiler doesn't care what goes where, but the Designer won't be able to make the jump from declarations in the .H file to definitions and initialization in the .CPP file. (I don't happen to like the WinForms Designer in VC++ very much, and try to avoid working with it as much as possible. I can't be the only one! Yes, it's very quick and easy to use -- it's also easy to break!)



 
 
GrkEngineer





PostPosted: Visual C++ General, C++/CLI Best Practices: .h versus .cpp files Top

Is this a change that we can expect in an upcoming service pack I would love to have designer look in the .h and .cpp file of same name and piece these together. I don't see this as very hard. MFC has been doing it this way. It would just require a level of abstraction in the C++/CLI implementation of the designer code. The C++/CLI version would have the smarts that aren't needed in C# and VB code generation. Please tell me this is in the works.


 
 
Simple Samples





PostPosted: Visual C++ General, C++/CLI Best Practices: .h versus .cpp files Top

Look at the headers for the STL. We use an #include to use the STL but we don't specify a library; all the code gets #included and compiled. That results in more efficient (inline) code and it makes things easier for us in terms of no libraries. The files don't have an extension, which the compiler does not care about, but that indicates a high degree of uncertainty. Right That is the way that C++ works, and if you want a sign from the C++ Gods, then it probably is a sign.

I don't like it; the code in the STL headers are difficult to understand and there many other things that I would prefer to be done differently. I think that instead of adopting the STL classes as they are, the C++ committee should have revised the standard so that there could be a better differentiation of definition and declaration and such.

Blame the C++ standards committee for not improving the language in more useful manner.