1

Writing a function in a .h file and its implementation right after (implicit inline), while using the virtual keyword:

virtual void g(){cout<<"is Inline?"};

Is the virtual functionality meaningless because the function is implemented in the .h? Is this considered to be an inline?

3
  • 2
    inline and virtual are unrelated... Commented Aug 23, 2012 at 11:44
  • 1
    @Synxis, not really. Suppose there exists a virtual function hierarchy and they are invoked using pointer/reference. Then it will restrict compiler to replace those function call with its contents (typical macro style inlining). Commented Aug 23, 2012 at 12:04
  • @iammilind: But inline has nothing to do with whether or not any particular use of the function is inlined (except to the extent that the compiler might use it as a hint). It means that the function is defined in every translation unit that uses it, which is unrelated to whether it's virtual. Commented Aug 23, 2012 at 12:34

3 Answers 3

7

Is the virtual functionality meaningless because the function is implemented in the .h?

No. virtual and inline are completely independent concepts.

virtual means that the function to call is chosen, at run-time if necessary, according to the dynamic type of the object it's invoked on.

inline means that you're allowed to define the function in more than one translation unit, and must define it in any translation unit that uses it. This is necessary (for some compilers) to allow the function to be inlined, but does not force all calls to be inlined. In particular, virtual calls usually won't be inlined (unless the dynamic type can be determined at compile time), so virtual will certainly retain its meaning here.

Is this considered to be an inline?

Yes, but (as mentioned above) that does not mean that all calls will be inlined.

Sign up to request clarification or add additional context in comments.

Comments

1

Is the virtual functionality meaningless because the function is implemented in the .h?

Nope. No reason to feel so. Header file is preprocessed and copy-pasted wherever it's included. So ultimately it's as good as implementing your g() in whatever .cpp file.

Is this considered to be an inline?

Yes. But here the inline doesn't mean usual interpretation of replacing function call with its content. virtual function resolution happens at runtime, so that can definitely not be inlined in that (macro style) way.
It means, that compiler guarantees to generate only 1 definition for all translation (.cpp file) units. Thus linker will not complain about multiple definition errors.

4 Comments

The inline has exactly the same meaning it has elsewhere. The compiler should try to implement the function inline. Sometimes it can't; in the case of virtual functions, of course, if it doesn't know the dynamic type of the object, it (probably) can't.
@JamesKanze: To add on that, if the class isn't used polymorphically, the compiler can safely inline if it can.
@JamesKanze, phresnel; usual meaning of inline (replacing function call with its text) is not guaranteed. It's compiler's decision. On the other hand, even if the function is not declared inline, compiler has still right to replace its function call with its text. Both are compiler dependent. The only guaranteed effect of inline is to secure ODR.
@iammilind §7.1.2/2 "The inline specifier indicates to the implementation that inline substitution of the function body at the point of call is to be preferred to the usual function call mechanism." There are no guarantees, but the intent is clear.
0

If you declare your function virtual, it is virtual, period. But, since virtual functions are usually selected at runtime, usually the compiler will not be able to inline them. If you call the function on an object, the compiler may inline it, since the call can be resolved at compile time. But it won't be able to inline a call through a reference or pointer, since it cannot resolve the dynamic type at compile time.

Take into account that neither the inline keyword not the implicit inlining here are mandatory for the compiler; they are just suggestions. But the virtual keyword is mandatory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.