Instead of using virtual functions where there is a lookup to the vtable pointer in the object, which then takes you to the vtable, containing a pointer to the function- would it not be possible to just contain a data member in the object which points directly to the function?
Possible to implement bypassing vtable for virtual functions?
778 Views Asked by user997112 At
1
There are 1 best solutions below
Related Questions in C++
- C++ using std::vector across boundaries
- Linked list without struct
- Connecting Signal QML to C++ (Qt5)
- how to get the reference of struct soap inherited in C++ Proxy/Service class
- Why we can't assign value to pointer
- Conversion of objects in c++
- shared_ptr: "is not a type" error
- C++ template using pointer and non pointer arguments in a QVector
- C++ SFML 2.2 vectors
- Lifetime of temporary objects
- I want to be able to use 4 different variables in a select statement in c ++
- segmentation fault: 11, extracting data in vector
- How to catch delay-import dll errors (missing dll or symbol) in MinGW(-w64)?
- How can I print all the values in this linked list inside a hash table?
- Configured TTL for A record(s) backing CNAME records
Related Questions in POLYMORPHISM
- Inheritance and Polymorphism in C#
- Downcast from a container of Base* to Derived* without explicit conversion
- Visual C++ - Virtual method is not overriden
- symbol(s) not found for architecture x86_64 c++
- laravel reverse polymorph issue
- polymorphic methods that return different class types in Swift?
- Polymorphic Data Translation/Conversion Design Pattern
- Why can't I call protected virtual base class function in the derived class overridden function?
- Best practice to set large amount of properties in C# base class from derived class
- Actual class of object reference
- Static and Dynamic Binding in Java
- Polymorphism - why not overloading instead
- I am having problems understanding inheritance and getting multiple classes to work together?
- Java polymorphism through injection at runtime
- Difference between runtime and compile time polymorphism in c++
Related Questions in VTABLE
- compiler's detail of this pointer and virtual functions
- understanding of multiple inheritance for c++
- Multiple Vtables ad VPointers in C++
- Accessing VTABLE directily issues undefined error
- Is the location of the virtual pointer in an object different if the object has polymorphism compared to multiple inheritance?
- Is virtual table necessary for C++?
- Link error missing vtable
- C++ Polymorphism. Why is this working?
- Possible to implement bypassing vtable for virtual functions?
- C++: How to look at vptr/ vtable contents
- How a single vtable is tracking new virtual functions?
- How to look up vtable index of a method on the IWinHttpRequest interface?
- Cache a lot of callback, then call them all batch-ly without v-table cost
- When a class has some virtual methods, do all it's methods use a vtable?
- How to properly solve the "undefined reference to vtable" error?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
If I understand your question, you are looking for a way to implement polymorphism using a function pointer.
Well, it is possible but extremely cumbersome, error prone, but it'll be difficult to outperform de generated by your compiler for virtual function calls.
How to do it ?
The idea is to use a function pointer. In order to implement polymorphims, it must be in the base class.
Code to test this construct:
Is it safe ?
There are severe limitations to this. For example:
Is it more performant than vtable lookup ?
No: look at the assembler executed for every polymorphic call to
itest():Of course, the optimizer could inline the code, removing some push and pop but the general principle is that the code for the indirection will be generated.
Isn't vtable lookup performant enough ?
Well a vtable lookup is basically loading a function pointer from a fixed offset calculated by the compiler. The assembler code for callling a vitual test() function would look like this:
Conclusion
A vtable lookup is at least as performant that a call through a function pointer. The compiler takes care of all the initialisation and the most complex inheritance situations. Better use the powerfull of virtual functions instead of trying to manually outperform your compiler.