Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!iuvax!cica!ctrsol!uakari.primate.wisc.edu!ames!attctc!mit-eddie!mit-amt!peter From: peter@mit-amt.MEDIA.MIT.EDU (Peter Schroeder) Newsgroups: comp.lang.c++ Subject: class members versus deriving Keywords: inheritance,efficiency,differentiating Message-ID: <608@mit-amt.MEDIA.MIT.EDU> Date: 27 Aug 89 00:39:53 GMT Organization: MIT Media Lab, Cambridge, MA Lines: 51 I would like to ask some help regarding a design choice I have to make. I've been writing a class of 3-vectors for a physical simulation system with the obvious arithmetic operators. Now, what I really want are row 3-vectors and column 3-vectors and I want to be able to differentiate between them. I do not want to include a type field, since the vectors are so fundamental and deep down in the code, that I want them to be as efficient as possible. On the other hand I want the compiler to flag the attempted addition of a row vector to a column vector as undefined. The choice I've made was to derive 2 classes from my base class vect: rvector and cvector. They do not contain anything but what the base class already contains. So far so good. Defining the arithmetic operators on, say, row vectors is now just a matter of calling the base class operators. However this entails extra copying since I can call the base class `+' with derived class objects (rvector) but the returned result is of type vect and still needs to be promoted up to rvector again. It makes sense that the language requires this, but in my case it seems silly, because there is no additional data in the derived class. Should I be defining rvector as a class with one member, vect? That would avoid this problem I suppose. Another thing I've been doing is to define the transpose operator: inline rvector& T( cvector& cv) { return ( rvector& )cv; } This way I can do things like T( c ) * c (dot product of cvector c with itself) but flag c * c. Am I taking advantage of a loophole here? The reason I did it this way was to avoid actually creating a new object, just treat the old one differently. This is even more of an issue with my matrices. I also define the notion of the transpose of a matrix by casting it into a tmatrix, which is just another derived class without anything extra. But now I can have the compiler differentiate between row major accessing and column major accessing ( as in transposing a matrix ) when I do a multiply with a vector, without actually having to make a new matrix and copying everything across. I just have to define two different functions for matrix * vector. Is that the right thing to do? You see, I am new to C++ and would really like to have someone look over my shoulders in cases like these... Peter peter@media-lab.media.mit.edu