Discussion why there is no built-in virtual function

Hi, I had a discussion with my friend asking why there is no built-in virtual function.

My answer is a built-in function at compile time, and a virtual function is mandatory in dynamic time. At compile time, it is not possible to know which function will call the virtual function. Therefore, the built-in virtual function is not a good idea.

However, he said that was not right.

His answer to the inline function has no address, so there is no way in the virtual table to put the address of the inline function, so there is no inline virtual function.

I would like to know three points:

  • Is my answer correct? I think my answer is right.
  • Does the built-in function have no address? I think he has an address;
  • even the built-in function has an address, is its answer better than mine?

Thank you very much!

+4
source share
1 answer

I do not like to answer.

inline functions are functions that can be defined more than once (although the definitions must match). This is why they can be placed in header files. They have an address, like any other function.

virtual functions are functions that can be overridden by derived classes.

You can have a function that can be overridden by derived classes and can have more than one definition:

 struct foo { virtual void f(); } inline void foo::f() {} 

However, any function can be built-in, i.e. its code is inserted at the dial peer instead of the actual call. This does not stop accepting his address. Even virtual functions not marked with inline can be inlined if the compiler can enable them statically.

+7
source

All Articles