Defining data in classes

Any rule of thumb to follow when it comes to this?
I've seen suggestions that say you should always define your functions/constructors/destructors in the class definition. Other sources say you should just do the prototypes in the class definition and then do ClassName::ClassFunction outside of the brackets.

I'm sure it comes down to personal preference, but for the more experienced programmers here, what have you most often encountered/liked to have seen?
Are there any pro's/con's with each style, for example as the project size increases, would one be better suited than the other?

Last edited on
I like inlining them (defining them inside the class) as it makes the classes able to be distributed as just a header file (no need to have a CPP file to compile) and it keeps things all together in one place for convenience.
Last edited on
I prefer separating classes into header and source files. The header just contains the class definition and the source file contains all of the member function definitions. Advantages with this is that you almost never have to care about problems with circular dependencies and it can also speed up compilation time.
Last edited on
Thanks for the tips, I think I am going to combine both your suggestions.

Compile time right now is not a big deal for my small <100 line programs, but I bet on bigger projects it would make sense to only compile the cpp files and not the header/class files.

Although, from what I've read inlining is up to the compiler if it thinks that you would gain a performance increase by pasting the code wherever the function is called.
I like to separate into header and source files. This way, another person can just look through your header files to get a feel for what's going on, without having to look at all the code for the implementation. This way they can also scan through headers for something in particular, find it, and then open the corresponding source file. Granted, you'll have more files to work with but it doesn't your main.cpp at all. But, if you separate them, you also have to change two files if you want to change some functions up. I think it's all just a matter of preference.
RB, that's what intelisense and right click -> go to definition are for ;)
Topic archived. No new replies allowed.