Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

penalty for "inlined" classes

Visual studio allow you to create "inlined" classes (if I am not mistaken with the name). So class header and implementation all in one file.

H. file contain definitions and declarations of the class and functions, there is no .cpp file at all.

So I was wondering if there is any penalty for doing it that way? any disadvantages ?

Thanks a lot

like image 399
kirbo Avatar asked Apr 07 '10 19:04

kirbo


2 Answers

any penalty for doing it that way? any disadvantages?

Yes. If you need to change the implementation of the class, since this is in a header file, all users of the class need to recompile, even though they should only be concerned with the interface. For some projects, this can be quite expensive.

like image 100
sbi Avatar answered Oct 09 '22 17:10

sbi


You can put the complete implementation of a class in the header with any compiler. There's usually a penalty in terms of compile time -- the header will be compiled separately for each source file that includes it.

There may be a penalty in terms of code bloat as well -- putting the function definitions inside the class definition implicitly declares them inline, so there may be an increased likelihood of the compiler generating code for each of them individually instead of generating code in one place, and generating calls to it elsewhere.

like image 24
Jerry Coffin Avatar answered Oct 09 '22 16:10

Jerry Coffin