Exforsys
+ Reply to Thread
Results 1 to 3 of 3

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?...

  1. #1
    Adrian is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    124

    Question What is the difference

    I want to know the differences between virtual functions and non-virtual C++ member function? Someone brief me on this?


  2. #2
    ashlee is offline Senior Member Array
    Join Date
    Apr 2006
    Answers
    108
    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.


  3. #3
    Sandhya.Kishannag is offline Member Array
    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



Latest Article

Network Security Risk Assessment and Measurement

Read More...