C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

virtual methods ?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Adie
Guest





PostPosted: Thu Jun 26, 2003 8:38 am    Post subject: Re: virtual methods ? Reply with quote



Adie wrote:

Quote:
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...

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

http://www2.dcs.hull.ac.uk/NETMSc/index.html

Back to top
Karl Heinz Buchegger
Guest





PostPosted: Thu Jun 26, 2003 12:17 pm    Post subject: Re: virtual methods ? Reply with quote





Adie wrote:
Quote:

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<>().

Sorry to be dense, but doesn't dynamic_cast<type id>(expression) just cast
expression to type id? Can you us it in an "if" statement?

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!


Quote:
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.

Might delegation also be used for this kind of problem, wrap the class
with the added functionality?

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
[email]kbuchegg (AT) gascad (DOT) at[/email]

Back to top
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
 


Powered by phpBB © 2001, 2006 phpBB Group