about dynamic binding

This is a discussion on about dynamic binding within the C++ Programming forums, part of the General Programming Boards category; Hi I am reading about virtual functions.I understand virtual functions that: * A member function of a class * Declared ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    about dynamic binding

    Hi

    I am reading about virtual functions.I understand virtual functions that:
    * A member function of a class
    * Declared with virtual keyword
    * Usually has a different functionality in the derived class
    * A function call is resolved at run-time

    but i don't understand its mechanism.Especially late binding or pointer assignment(from derived to base class).This is code:
    Code:
         class Window // Base class for C++ virtual function example
         {
           public:
              virtual void Create() // virtual function for C++ virtual function example
              {
                   cout <<"Base class Window"<<endl;
              }
         };
    
         class CommandButton : public Window
         {
           public:
              void Create()
              {
                  cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
              }
         };
    
         void main()
         {
             Window  *x, *y;
      
             x = new Window();
             x->Create();
    
             y = new CommandButton();
             y->Create();
         }
    
       The output of the above program will be,
                     Base class Window
                     Derived class Command Button
    Here when we remove virtual.
    The output of the above program will be,
    Base class Window
    Base class Window

    I want to learn What is happening in that line:y = new CommandButton();
    We assign a commandbutton object to y but if we don't write virtual before function ,it calls base class's function.If we write virtual it calls derived class's func.
    I don't understand this assignment.doesn't new operator return object's address?So what happened behind the code?
    I hope i can explain my problem.I am really confused.
    Any help will be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    y is a Window pointer, not a CommandButton pointer, so while its dynamic type is CommandButton*, its static type is Window*. At the point in main where the code says y->Create(), the program first looks at the static type. So if Create() is not virtual, then Window's Create() method will be used. What the virtual keyword does is to tell the program to check the actual dynamic type of what y points to, and find the most appropriate override to call based on that type. Without the virtual it doesn't even check the dynamic type. In the case of y, it finds that the dynamic type of y is CommandButton, and CommandButton overrides Create(), so it calls CommandButton's version of Create.

    As far as how the dynamic type is actually remembered when the pointer is saved, that depends on the implementation.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Without the "virtual" keyword, the variable type determines the function that is called. In this line:
    Code:
    y = new CommandButton();
    y is of type Window*, so Create() in Window is called. That process is called "static binding".

    With the "virtual" keyword, the type that is assigned to the variable determines the function that is called. Since a pointer of type CommandButton* is assigned to the variable 'y', Create() in the class CommandButton is called. That process is called "dynamic binding".

    The static type of a pointer variable is its type in the declaration statement. The dynamic type of a pointer variable varies according to the type of the object it points to. When you use the "virtual" keyword, you are asking that the dynamic type be considered instead of the static type. Then, it's as if there is an internal switch statement, and depending on what the dynamic type is, a different function is called.
    Last edited by 7stud; 02-23-2006 at 12:12 PM.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    It goes something like this:

    Every class is a type. For every type that has at least one virtual function, the compiler generates a VTABLE (virtual table). You could create hundreads of objects of a paticular type, but only one VTABLE will exist for all those objects of the same type. When you derive from base (type), the same mechenism in the abovementioned also applies to the derived class type. When you override an inherited virtual function, the compiler will generate a VTABLE for the derived class too. It will place the memory address of your version of the virtual function, in the slot of the Derived type's VTABLE where the base virtual function would have been if you had not overriden it (or reintroduced it).

    When you call a function with a base pointer that holds a derived object, the runtime will look into the object's type (Derived) VTABLE *if* the function being called is virtual (which it is in this case because a function that overrides a base virtual function is itself implicitly virtual). If not, then it will use the base pointer's VTABLE (if you don't reintroduce: use a name for a function that's the same in the base class but not override it), which would use the base version of the virtual function and thus, that's how the magic works.

    Base pointer and Derived object:
    Base *basePtr = new Derived();

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Thanks for all answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boost::shared_ptr and dynamic binding
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 03:50 PM
  2. Dynamic binding
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 07-09-2006, 04:37 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 08:01 AM
  4. dynamic binding
    By freethenet in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-26-2004, 03:31 PM
  5. Static Binding & Dynamic Binding :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2001, 07:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21