Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-07-02 08:52:53


On Tuesday 02 July 2002 04:35 am, David Abrahams wrote:
> > textbooks would tell us to write the object hierarchy like this:
> >
> > class Expression { /* ... */ };
> > class Plus : public Expression { /* ... */ };
> > class Minus : public Expression { /* ... */ };
> > class Literal : public Expression { /* ... */ };
> >
> > Why? Because Plus is-a(n) Expression, and a Literal is-a(n) Expression.
>
> 'xcept it ain't. There is no is-a relationship with variant. If there was,
> I'd be able to pass a double* where an expression* was expected.

Do you really want to limit the definition of 'is-a' to C++ inheritance? That
would say that dynamic polymorphism is impossible via any other mechanism. I
would say that:

  struct some_type {
    void visit_me(const boost::function<void, some_type*>& visitor);
  };

is every bit as polymorphic as:

  struct visitor_interface {
    virtual void visit(some_type*) = 0;
  };

  struct some_type {
    void visit_me(visitor_interface* visitor);
  };

but you wouldn't agree?

> Structurally speaking you've got it inside-out, in fact. Given an
> expression lvalue, we can get a double lvalue.

Given a double, we can build an expression from it (derived-to-base cast).
Given an expression that is really a double, we can get to the double
(base-to-derived cast). What's inside-out?

> > When we know the type dynamically, we cast to that
> > type, just like we would dynamic_cast in the OO style.
> >
> > PyObject*, variant, any -- they're all just union types. Is a union a
> > container of one?
>
> Yes! A union definitely does *not* present a logical is-a relationship.
>
> -Dave

A C-style union is a bad example (at least, for my side of the fence) because
it feels syntactically like a container. If we step away from C++: would you
say that the is-a relationship holds in an ML variant? e.g.,

  type expression = Literal of float
    | Plus of expression * expression
    | Minus of expression * expression
  

Does it hold that Plus(x, y) is-a expression?

        Doug

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk