Possible Duplicate:
Pure virtual functions may not have an inline definition. Why?
Just as the title says, why? And how about (not pure) virtual function, can it be inline format? And I’d like to know the real reason. Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A virtual function 99% of the time cannot be
inlinedbecause the function pointer is resolved at runtime usingvtable.The purpose of a virtual function is to be overloaded by subclasses.
An example :
The main function will return 2 and not 1.
If the compiler inline the function it will return 1 because the compiler can only assume that the type of
*aisAand notB. So the compiler will not do it if he cannot assume safely the type of*a.In this example the compiler may successfully and safely assume that virtualization is not needed : This is really depending of the compiler and of optimization level.
In some case the compiler can safely assume that virtualization is not needed and only in these cases the inline keyword makes sense.
Even if you declare a function with the keywords
inline, the function may not be inlined.Anyway adding manually the
inlinekeyword is most of the time not a good idea, compiler today are good and automatically inline function when necessary.By adding
inlineyou may in some case decrease performance, so it a good practice to not abuse of it