C++ inline and inheritance

Started by
2 comments, last by stonemetal 16 years, 3 months ago
Greetings, Are there any quirks to using inline on class member functions and (multiple) inheritance that I should know about? Any gotchas that I might not know of? I'm wondering about this because of two reasons. A while back I got stuck for a while when a base class had an overloaded member function. In a derived class I tried to override just one version of the overloaded function, but that didn't work. If you override one member function in a derived class, it will hide the others if you don't also override them. This, along with the fact that you could declare the function inlined in the base class but can choose not to in the derived class (if you are overriding it) or vice versa, it got me worried that there might be a pitfall in there somewhere that I'm not aware of. I took a look at my C++ reference book to see if it had anything to say in depth about class inheritance and inlined member functions and didn't find much other than how to write the syntax. I also didn't see much in the C++ FAQ Lite. Any insight would be greatly appreciated.
Advertisement
Inline keyword denotes optional operation for the compiler. If it cannot inline, it won't.

It's also mostly redundant, compilers inline aggressively as it is, so it's rarely needed to specify it directly.

Quote:This, along with the fact that you could declare the function inlined in the base class but can choose not to in the derived class


It doesn't really matter how you declare it. You cannot force compiler to inline a certain function (in C++ at least, compilers may allow it).

But in general, you should go easy with inline, compiler will often know better what to inline, and have better overview of context.
If you want to unhide a name from a base class, just put
using Base::f;
in your derived class definition.

The reason the name is hidden is because name lookup terminates as soon as a suitable name is found, and overload resolution is done in a separate phase. The using directive pulls the other overloads from the base into the derived class namespace, so that they will be found in phase one.

To make it is hell. To fail is divine.

if you are making a polymorphic call it can not be inlined since the correct function to call is not known until runtime.

This topic is closed to new replies.

Advertisement