DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: virtual functions & name hiding

  1. #1
    Join Date
    Dec 2004

    virtual functions & name hiding


    Write a class with three overloaded virtual functions. Inherit a new class from this and override one of the functions. Create an object of your derived class. Can you call all the base class functions through the derived-class object? Upcast the address of the object to the base. Can you call all three functions through the base? Remove the overridden definition in the derived class. Now can you call all the base class functions through the derived-class object?


    The answers are; yes, yes, yes! Virtual functions are inherited. Only if you override one will it replace the inherited one. The compiler generates the virtual function table with the most-derived functions that apply to a class, as you would expect. In the example below, if you were to remove the definition of Derived::f( ), then Base::f( ) would be called automatically.
    //: S15:InheritVirtuals.cpp
    #include <iostream>
    using namespace std;
    class Base {
        virtual void f() {
            cout << "Base::f()\n";
        virtual void g() {
            cout << "Base::g()\n";
        virtual void h() {
            cout << "Base::h()\n";
    class Derived : public Base {
        virtual void f() {
            cout << "Derived::f()\n";
    int main() {
        Derived d;
        Base* bp = &d;
    /* Output:
    Firstable, I can see that virtual functions in the base class are NOT overloaded in this code. (here are 3 different functions instead)
    The next thing, in main, I can see just the calls by upcasting. There's no common calls by derived-class object, like d.f().

    Well, if these virt.functions had been overloaded and than one of them overrided(in derived-class), it couldn't be possible to access any of the base class functions through the derived-class object anymore, ok? (because of name hiding)

    On the other hand, if I upcast the address of the object to the base, I could access those base functions which are not overrided in derived-class, and for that which is overrided, version from derived class would be called, ok?

    Finally, if I remove the overridden definition in the derived class, I can access all the base class functions through the derived-class object, ok?

    So, my answers would be no, no, yes;



  2. #2
    Join Date
    Jan 2005
    you still CAN access the base class methods through something like


    which explicitely calls the Base-method.
    So the answer to the first question should be "Yesno". The author probably wanted to explain overriding and in that sense, the first answer should be no.
    The other two answers are correct, I think.


  3. #3
    Join Date
    Nov 2003
    first, there's a difference between oveloading (which isn't present in yourcode) and overriding, which is the process of defining a virtual member function in a derived class.
    The answers to all the three questions are yes, as you said, but these questions are porrly phrased. The author doesn't explain whether access to the member functions is allowed by what is known as qualified call, e.g., df->Base::func();

    Using a qualified call overrides the virtual function dispatching mechanism so unless the functions are declared private, you can always access them from any derived class.
    The author also doesn't explain how to access these functions: is it through a pointer/reference or by means of an object? In the latter case, the virtual mechanism is also disabled:

    Base b;
    b.f(); //calls B::f, never d::f
    Last edited by Danny; 04-04-2005 at 04:25 PM.
    Danny Kalev

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center