Mail Archives: djgpp/1999/01/21/00:37:55
From: | "Larry Brasfield" <larry_br AT sea_net DOT com>
|
Newsgroups: | comp.lang.c++,comp.os.msdos.djgpp
|
References: | <36A68B87 DOT FFBA110C AT ohriki DOT t DOT u-tokyo DOT ac DOT jp>
|
Subject: | Re: How to implement "They are all Vectors, but different realities?"
|
Lines: | 54
|
Organization: | Serendipitous Endeavors
|
X-Newsreader: | Microsoft Outlook Express 4.72.3155.0
|
X-MimeOLE: | Produced By Microsoft MimeOLE V4.72.3155.0
|
Message-ID: | <Lgwp2.660$GN.120@news.rdc1.wa.home.com>
|
Date: | Thu, 21 Jan 1999 02:27:55 GMT
|
NNTP-Posting-Host: | 24.0.239.100
|
X-Complaints-To: | abuse AT home DOT net
|
X-Trace: | news.rdc1.wa.home.com 916885675 24.0.239.100 (Wed, 20 Jan 1999 18:27:55 PDT)
|
NNTP-Posting-Date: | Wed, 20 Jan 1999 18:27:55 PDT
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Shue-Cheng CHEN wrote in message <36A68B87 DOT FFBA110C AT ohriki DOT t DOT u-tokyo DOT ac DOT jp>...
>Hi!
Hi.
> We know "Force" and "Velocity" are both "Vectors" in mathematics, so
>I defined them to own all behaviors and data of Vector (has been defined),
>without duplicating the code, as follows,
>
> class Force : public Vector {...};
> class Velocity : public Vector {...};
>
>Therefore, the operators of Vector, for example, operator+(Vector&, Vector&)
>can be used for Force and Velocity, like
>
> Force + Force // Ok
> Velocity + Velocity // Ok
>
> But "Force" and "Velocity" are different physical realities, so
>
> Force + Velocity // Wrong
>
>I would like to deploit the common implementation of them to avoid
>duplicating their code, but how should I do to prevent from Force +
>Vector meaningless operation?
Several approaches can be taken:
1. Make the implementation of Vector::operator+
protected and write Force::operator+ (and the
others) to simply forward the operation to that
protected member. This would give you compile-
time type-safety.
2. Use RTTI within Vector::operator+ to verify, at
runtime, that the objects have the same type.
3. Use double-dispatch to permit an arbitrary
set of combinations to succeed and fail the
rest. This would require specific code for
each combination, and might be distasteful
depending on how open Vector is to be.
But it offers the most flexibility.
4. It would be possible to put allowed combinations
into a map that would be consulted at each Vector
add. This would be more maintainable but slower.
--Larry Brasfield
Above opinions may be mine alone.
(Humans may reply at unundered larry_br AT sea_net DOT com )
- Raw text -