Thread: virtual function

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    virtual function

    Hello everyone,


    In the case when function in derived class is virtual, but function in base class is not virtual, when we make base class pointer pointing to derived class' instance, the function in base will be invoked.

    I am confused why not the function in Derived class is invoked (this is my question).

    When making base pointer pointing to derived class' instance, the layout of derived class should begin with vtable of functions of derived class (virtual function of foo in Derived class in the sample), and the base pointer is pointing to the layout, so when invoking the derived class version function could be found in vtable and it should be invoked. How compiler process this situation internally?

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Base {
    public:
    	void foo()
    	{
    		cout << "Base foo " << endl;
    	}
    };
    
    class Derived: public Base {
    public:
    	virtual void foo()
    	{
    		cout << "Derived foo " << endl;
    	}
    };
    
    int main()
    {
    	Derived d;
    	Base* pb = dynamic_cast<Base*>(&d);
    	pb->foo(); // output Base foo
    
    	pb = static_cast<Base*>(&d);
    	pb->foo(); // output Base foo
    
    	pb = &d;
    	pb->foo(); // output Base foo
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    when compiler looks at the base class pointer it uses the base class layout.
    non-virtual functions (in the current class) are called directly
    virtual-functions - are called using pointer in the VT

    so because in the base class the function is not virtual - there is no use of looking for VT to search it there
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks vart,


    My question is answered.

    Quote Originally Posted by vart View Post
    when compiler looks at the base class pointer it uses the base class layout.
    non-virtual functions (in the current class) are called directly
    virtual-functions - are called using pointer in the VT

    so because in the base class the function is not virtual - there is no use of looking for VT to search it there

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Stroustrup Talk on C++0x
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 07-20-2007, 02:02 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM