Ryan's District
November 22, 2008, 03:35:40 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Ryan's District Lottery: Claim your ticket or check
Jackpot details  
 
   Home   Help Search Chat Calendar Chess Shop Login Register  
Digg This!
Pages: [1]   Go Down
  Send this topic  |  Print  
Author Topic: C++ tips - Type-tests, lists, and switch statements  (Read 4338 times)
0 Members and 1 Guest are viewing this topic.
ben2ong2
Quality Poster
Paid
Hero Member
*****

Reputation: 17
Offline Offline

Gender: Male
Posts: 2374
9976.80 RD$

View Inventory
Send Money to ben2ong2

View Profile Awards
« on: October 07, 2006, 08:48:12 AM »

PROBLEM: caj20721@uxa.cso.uiuc.edu (Chris Jasek)

Let's say I have a class hierarchy:

         Animal
         /  \
        /    \
      Dog    Cat

And then I create a list of Animals that contains Dog and
Cat class objects.  Of course when I get an item from the
list it must be returned as the base class type i.e. Animal.
From this pointer to an Animal that is returned there is no
way to access specific methods in either the Dog or Cat class.


RESPONSE: cvale@netcom.com (Chris Vale)

Assume Animal, Dog, and Cat have a memeber called isA. Then
Animal a = GetFromList();
switch( a.isA() ) {
CAT:
   do cat stuff
   break;
DOG:
   do dog stuff
   break;
}


RESPONSE: rmartin@rcmcon.com (Robert Martin), 17 Sep 93

The current standard provides a feature very similar to isA so that you can
ask an object what type it is.  This is sometimes useful.   However if
used generally, it leads to design problems.

In the Animal/Cat/Dog structure above, are there any common methods in
Animal?  If not, then Animal probably does not deserve to be a base
class.  If so, then the users of a list of Animal objects should
confine themselves to those methods.  If you have the need to operate
specifically upon Cats and Dogs, you should probably create a list of
Cats and a seperate list of Dogs

Let me stress that one of the fundemental benefits of OOP is the
ability to eliminate the kind of typeswitching shown in the example
above.  Such switches are maintenance bottlenecks since they must
constantly be found and updated whenever some new kind of animal is
added to the program.

If you find yourself needing switch statments to test the type of an
object, then you should re-think your design.  Try to determine a
class hierarchy and abstract interface which will make the switch
irrelevent.  This is the *hard* part of OOD.

Logged

You are not allowed to view links.
Register or Login
Free Paid Survey & Home Business Resources.

You are not allowed to view links.
Register or Login
Free Article Directory | Quality Content
Ryan's District
« on: October 07, 2006, 08:48:12 AM »

 Logged
Pages: [1]   Go Up
  Send this topic  |  Print  
 
Jump to:  

Archive - WAP2 - WAP - imode
Sponsors: RAYAN.tv
-

Powered by MySQL Powered by PHP Powered by SMF 1.1.7 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 2.24 seconds with 30 queries.

Google visited last this page Today at 06:34:21 AM