are@sinix.UUCP (Andreas Eder) (06/05/91)
Hello experts, I'm new to C++ and have the following problem: I have several numeric types such as integers, rationals, reals etc. Now I want a procedure to add to numbers that can add two numbers regardless of the actual type. That is in some place of the code I have number *a, *b; where number is the base type of all the integers, rationals, etc.. Now I want a function number *add(number *, number *) that can add any two numbers. I now about virtual functions, but wouldn't that only work depending on the type of the first variable. Now, how do I do this without switching on the type of the second parameter ? Or do I have to include a type field in the class definition? Any help is welcome. Thank you, Andreas are@portland.sp4n1.siemens.de
mittle@blinn.watson.ibm.com (Josh Mittleman) (06/07/91)
This is no small problem that you've encountered; several other recent postings were other version of the same thing. What you are looking for is multiple polymorphism, i.e., the ability to have a virtual function depend on the types of more than one of its arguments. C++ does not provide this feature. There are way around it, but none of them are particualrly satisfying. A good discussion of the subject, particularly addressing the domain of interest to you, can be found in "Generalized arithmetic in C++", by Timothy A. Budd, which appears in the Feb. 91 issue of the Journal of Object-Oriented Programming, pp.11-22. =========================================================================== Josh Mittleman (mittle@watson.ibm.com or joshua@paul.rutgers.edu) J2-C28 T.J. Watson Research Center, PO Box 704, Yorktown Heights, NY 10598