derived classes and virtual function


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: derived classes and virtual function

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    derived classes and virtual function

    Hi, when use virtual functions you can just use a base class pointer access and of those functions in any derived type right?? But is it possible to use the base type pointer to access member functions specific to the derived type??? or would you then need a specific derived type pointer???

    If you do need a specific derived type pointer, doesn't that severly limit polymorphism and the use of virtual functions??

    Again, if that's the case, is it really anymore useful than simple function overloading other than saving a few lines of code??



    Thanks
    Alex

  2. #2
    Join Date
    Mar 2004
    Posts
    78
    Well, the idea with all the virtuality and polimorfic things is to let you change the behavior of a class. You make it by inheriting it and defining the same methods as this in the base class but with new behavior. By doing so you will use the same base class and you will have different behavior for all different types of objects. The classic examples is with the base class CFigure and the sublclasses CRectangle, CTriangle and so on. These are different shapes but they have common methods - getPreimeter and so on... By polimorfism you'll use one and the same base class and you'll have different ways to compute the perimeter - specific to the type of object you have.

    SO:

    1. When you have the base pointer and you use it - because of the overridding you actually call the inheriting class methods. If you want to call the original methods use something like this: pFigure->CFigure::getPerimeter(...).

    2. If you have pointer to inhreting class i.e. CSquare you will easily call the base methods by: ( (CFigure*)pSquare )->getPerimeter(...) . If you call pSquare->getPerimeter(...) you will have the CSquare::getPerimeter(...) if it is not inherited

    IF YOU WANT TO SPECIFY WHICH METHOD YOU NEED specify the class that has defined it.

    Well there is another problem - do the inheriting class exist?

    To solve the problem use dynamic down cast - by this you will be sure that you do not have pointer to an object of type CFugure but a pointer to an object of type CSquare for expample casted to the base class.( CFigire* pFigure = new CSquare(..);)

    The dynamic cast will let you to safely downcast and use the subclass methods. If it is a pointer to the base class object you will have an error.

    That's it.... if it is hard to understand what I wrote, ask again or write to:
    plstefanov@hotmail.com

    Enjoy coding.... there is no limit :))

    Mr.PLS

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    First of all, polymorphism works when you use a pointer or reference. By using a pointer or reference to a polymorphic base class, you call a member function that the base class declares. This doesn't mean that that specific function will be called at runtime. Instead, the implementation will look for the most derived version of that function which is defined in class X, where X is the dynamic type of the pointer. This leads us to an important distinction between static types and dynamic types. Take for example:

    Code:
    struct Base
    {
    virtual int f();
    };
    
    struct Derived : Base
    {
     int f();//overrides Base::f
    };
    Now consider this pointer declaration:

    Code:
    Base * p; //static type of p is 'Base *'
    p = new Derived;// dynamic type is 'Derived *', though
    When you call p->f() the decision which f() should be invoked is deferred to runtime. At runtime the implementation looks for the appropriate f() that matches the dynamic type of p, i.e. Derived. Consequently, it will invoke Derived::f(), not Base::f(). To override this dynamic binding, you can either use a qualified call: p->Base::f() or simply use value semantics:
    Code:
    Derived d;
    Base b;
    b.f(); //always resolved to B::f()
    I'm not sure I understand what you meant by calling members of a derived class from a pointer to Base. Do you mean something like this:

    Code:
    struct Base
    {
    virtual int f();
    };
    
    struct Derived : Base
    {
     int f();//overrides Base::f
     int g(); //a new function
    };
    
    Base * p = new Derived;
    p->g(); //illegal
    You can't do that because the compiler can't find a member function called g() in the class Base. To do this you would need to use dynamic_cast.
    Danny Kalev

  4. #4
    Join Date
    Mar 2004
    Posts
    78
    Hi again,

    As aalways I fully agree with Dany but as I read his answer I started to wonder:
    Did I say something wrong?
    I am not sure if the quality of my english is high enough but I meant just the same. Or there is really something wrong.

    So Dany, if there is something wrong in my post - say it! I am happy to learn something new.

    Mr.PLS

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    No, you didn't say something wrong. However, I wanted to add a few missing bits such as references and the use of a qualified call to override the dynamic binding mechanism. Don't worry -- I'm usually pretty outspoken when I find a factual error:)
    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