Thursday, July 8, 2010

A gratuitously cute name.

I'm reading Effective C++ 3e and in the OO section Mr. Meyers has one point he wasnt people to remember: The battle of Hastings was in 1066... OK, the real bullet point is that public inheritance models "is-a". To illustrate his point he uses the example that a Penguin is-a bird, but then goes on to point out that this can get you in trouble if you assume that all birds can fly. Well, not all can and several ways to deal with this are brought up including one that brings me to my point:

class Penguin: public Bird {
public:
virtual void fly() { error("Attempt to make a penguin fly!"); }
};

This struck me a remarkably funny, particularly if you include exception handling:

virtual void fly() { throw new PenguinError("Attempt to make a penguin fly!"); }

Q: How do you attempt to make a penguin fly?
A: You throw it.

Q: Well how does that work out for ya?
A: The penguin seems to be 'in flight' for a bit and then crashes.

Right now I'm really regretting that I can't use exceptions at work because I can't wait to derive a Penguin class from Exception just for that case!

No comments:

Post a Comment