Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!bu.edu!m2c!umvlsi!dime!connolly From: connolly@livy.cs.umass.edu (Christopher Connolly) Newsgroups: comp.lang.c++ Subject: Re: asking an object for its type Message-ID: <27114@dime.cs.umass.edu> Date: 24 Feb 91 20:21:35 GMT References: <1991Feb19.000449.22255@gpu.utcs.utoronto.ca> <607@taumet.com> <1991Feb22.195826.14008@gpu.utcs.utoronto.ca> <27C6DBF3.28A3@tct.uucp> Sender: news@dime.cs.umass.edu Reply-To: connolly@livy.cs.umass.edu (Christopher Connolly) Organization: University of Massachusetts, Amherst Lines: 27 In article <27C6DBF3.28A3@tct.uucp> chip@tct.uucp (Chip Salzenberg) writes: >If the "same" operation is actually several operations that are >distinct in semantic terms (as opposed to implemention), then folding >them together into one virtual function is a design error. ^^^^^^^^^^^^ I don't think this is necessarily the case. For example, I am implementing, in C++, polynomial arithmetic over (somewhat) arbitrary coefficient fields. Since I have little control over how the user might wish to express those coefficients, I have to be prepared for quite a few combinations of coefficient types (ints, extended precision ints, rationals, algebraic numbers, other polynomials, etc.). Moreover, I have to check these coefficient types at runtime (as the user types the expressions in). Consider the operation "+": I can use simple integer addition if both my coefficients are integers. The "+" operation results in an integer in this case. On the other hand, adding an integer to an algebraic number usually results in another algebraic number. Integer addition and algebraic number addition are quite different algorithms, producing quite different results. Since integer arithmetic is much cheaper than algebraic number arithmetic, it would be a design error for me to coerce all the results of addition to algebraic numbers. I see no way around defining a generic "+" operation which takes different data types, and returns different data types as a result (all derived from the "coefficient" class, of course).