virtual functions & name hiding


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
    Posts
    47

    virtual functions & name hiding

    15-22

    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?

    Solution:

    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.
    Code:
    //: S15:InheritVirtuals.cpp
    #include <iostream>
    using namespace std;
    
    class Base {
    public:
        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 {
    public:
        virtual void f() {
            cout << "Derived::f()\n";
        }
    };
    
    int main() {
        Derived d;
        Base* bp = &d;
        bp->f();
        bp->g();
        bp->h();
    }
    
    /* Output:
    Derived::f()
    Base::g()
    Base::h()
    */
    ///:~
    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;

    Correct?


    TVM

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Hi;
    you still CAN access the base class methods through something like

    bp->Base::f();

    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.

    Dieter

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

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