Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Linking with Methods Defined in the Class Definition

For curiosity's sake, if you put the method definition inside the class definition in the header, and the compiler doesn't inline it, then what object file or files is that method put into for accessing during the linker phase? Is it put in every .obj file that includes the header, and then extra copies are thrown away during the linker phase?

like image 265
Anthony Johnson Avatar asked Feb 25 '23 00:02

Anthony Johnson


2 Answers

Is it put in every .obj file that includes the header, and then extra copies are thrown away during the linker phase?

In general, yes. See this paper.

like image 183
Nemo Avatar answered Mar 08 '23 08:03

Nemo


It depends on the compiler, but GCC at least will try to place it in the same object file as the first non-inline member. If all members are inline, then there will be a copy in every object file that requires the function (not necessarily all files that include the class definition), and the extra copies are thrown away during linking.

like image 27
Mike Seymour Avatar answered Mar 08 '23 10:03

Mike Seymour