virtual methods ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adie

    #16
    Re: virtual methods ?

    Adie wrote:
    [color=blue]
    >Heh there's a UK university doing an MSc .NET, there's nothing like a well
    >rounded education, and that's nothing like a well a well rounded
    >education...[/color]

    For the record it's ".NET MSc in Distributed Systems Development"


    Comment

    • Karl Heinz Buchegger

      #17
      Re: virtual methods ?



      Adie wrote:[color=blue]
      >[color=green]
      > >etc. This allows you to store all of your derived objects in a
      > >base_object pointer, or vector of pointers, or whatever. Each branch
      > >then has to be identified at its branch point using dynamic_cast<>( ).
      > >So if I have a vector full of base_object*s and I need an interface
      > >that's only provided by objects descended from derived1b, I can filter
      > >the vector with dynamic_cast<>( ).[/color]
      >
      > Sorry to be dense, but doesn't dynamic_cast<ty pe id>(expression) just cast
      > expression to type id? Can you us it in an "if" statement?[/color]

      The trick with dynamic_cast ist the following:
      dynamic_cast also checks if the object you want to cast is really
      of the type you specify in the dynamic_cast. If you use it with
      pointers and the object is not of the required type you will
      get a 0 Pointer. And that is something you can detect.

      example:

      for (int i = 0; i < vBase.size(); i++)
      {
      Base* newBase;
      newBase = vBase[i];

      // now newBase could point to a Base object or to
      // DerivedA or DerivedB objects. Figure out which one

      // try to cast newBase to a DerivedA pointer.
      // But at runtime it should be checked, if the object newBase
      // points at is really a DerivedA

      DerivedA* pPtrA = dynamic_cast< DerivedA* >( newBase );
      if( pPtrA != 0 ) {
      // it was a DerivedA object
      }

      else {
      DerivedB* pPtr = dynamic_cast< DerivedB* >( newBase );
      if( pPtr != 0 ) {
      // it was a DerivedB object
      }
      else {
      // must have been a Base object
      }
      }
      }

      Now compare that with the simple use of a virtual function :-)

      Additionally. Imagine you introduce a new class in about half a year:

      class DerivedC : public Base
      {
      ...
      };

      What do you have to do? Well, in the above you have to add some logic
      for the new class to detect it and add another if - else branch.

      And with the dispatch through virtual functions?
      Surprise: You have to do nothing! You just implement the function f()
      in the new class and the code you already had:

      for (int i = 0; i < vBase.size(); i++)
      {
      Base* newBase;
      newBase = vBase[i];
      newBase->f();
      }

      dispatches to DerivedC::f() whenver there is a pointer to DerivedC
      stored in the container. You don't even have to recompile the above
      loop!

      [color=blue][color=green]
      > >It has its uses. It can simplify certain aspects of your programs, but
      > >overuse can make your code that much more difficult to read. Usually
      > >it's preferable to have a more complete interface in the base and
      > >provide implementation details in the descendants.[/color]
      >
      > Might delegation also be used for this kind of problem, wrap the class
      > with the added functionality?[/color]

      Since the goal is to store pointers in a container and use
      polymorphism, this is not a good idea. If you want to make
      use of polymorphism to your advantage, then creating a complete
      interface in the base class is often far less work then any other
      option.

      --
      Karl Heinz Buchegger
      kbuchegg@gascad .at

      Comment

      Working...