
- Forum
- Programming Talk
- C and C++
- What is the difference
What is the difference
This is a discussion on What is the difference within the C and C++ forums, part of the Programming Talk category; I want to know the differences between virtual functions and non-virtual C++ member function? Someone brief me on this?...
-
What is the difference
I want to know the differences between virtual functions and non-virtual C++ member function? Someone brief me on this?
-
Virtual function is a mechanism to implement the concept of Polymorphism an ability to give different meanings to one function. The main difference between a non-virtual c++ member function and a virtual member function is the way both gets resolved. A non-virtual c++ member function gets resolved during compile time also called as static binding but in contrast Virtual Functions are resolved during run-time also called as dynamic binding.
-
02-24-2012, 06:18 AM #3
- Join Date
- Feb 2012
- Answers
- 66
virtual member functions are resolved dynamically (at run-time). That is, the member function is selected dynamically (at run-time) based on the type of the object, not the type of the pointer/reference to that object. This is called "dynamic binding." Most compilers use some variant of the following technique: if the object has one or more virtual functions, the compiler puts a hidden pointer in the object called a "virtual-pointer" or "v-pointer." This v-pointer points to a global table called the "virtual-table" or "v-table."
The time-cost overhead is also fairly nominal: compared to a normal function call, a virtual function call requires two extra fetches (one to get the value of the v-pointer, a second to get the address of the method). None of this runtime activity happens with non-virtual functions, since the compiler resolves non-virtual functions exclusively at compile-time based on the type of the pointer.
-
Sponsored Ads

Reply With Quote





