Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are inline constructors and destructors not a good idea in C++?

I remember reading in one of the C++ books (quite some time ago) that it is not a good idea to have inline Constructors and Destructors especially for derived class. I understand that inlining would induce some bloating up of object code but are there any other design considerations that discourage inline constructors and destructors? Of course most compilers may reject the inline and proceed with creating a function body but if they were to inline what penalty might one have to pay?

like image 860
Nikhil Nehriya Avatar asked Aug 21 '11 12:08

Nikhil Nehriya


People also ask

What are the disadvantages of an inline function?

5) Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed. 6) Inline functions might cause thrashing because inlining might increase size of the binary executable file. Thrashing in memory causes performance of computer to degrade.

Can constructor be inline?

Constructors may be declared as inline , explicit , friend , or constexpr . A constructor can initialize an object that has been declared as const , volatile or const volatile .

Why large functions should not be made inline?

If you convert larger functions to inline, it may lead to code bloat and reduce the functioning quality of the program. Always try to define large functions outside the class, since functions defined inside a class are automatically defined as inline and this will affect the program negatively.

Why is it a good practice to use destructors in a program?

It is a good practice to make the base class's destructor as virtual as this ensures that the object of the derived class is destroyed properly. Whenever a virtual class is used, a virtual destructor should be added immediately to prevent any future unexpected results.


1 Answers

The compiler is free to inline code that you have not declared inline, and is free not to inline code that you have declared inline. I have seen compilers do both of these things. Because of this the inline keyword does not mean what most people think it does. It's meaning is to allow an exception to the one definition rule, so you can put functions etc. in a header file and not get linker errors.

So the advice is rubbish, let the compiler decide what is best to inline and what is not. Put inline where you need it to prevent linker errors, that is all.

like image 179
john Avatar answered Oct 01 '22 20:10

john