Sign in to follow this  

C++ inline and inheritance

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this