Topic: Classes with "indirection
Author: miguelc@saguaro.eecs.berkeley.edu (Miguel Corazao)
Date: 25 Feb 1994 22:06:13 GMT Raw View
I am writing this article to share a grip about C++ and seek expert info as
to whether a solution has been proposed in for the standard.
One major source of frustration I have had in C++ is the inability to simulate
repeated indirection adequately (I am told the features I am looking for exist
in other OO languages. I don't know the formal terminology). As an example,
I have created sparse matrix classes in which I tried to make the interface as
close to that of an array as possible. The problem is that for each level of
"indirection", I have to declare a new class even though all the classes are
the same essentially.
Let me illustrate my point. Here is a sample piece of code using a proposed
syntax (note: I just made the syntax up. There's probably something better
that could be used).
class SparseMatrix($) {
...
public:
SparseMatrix($) SparseMatrix($*int);
SparseMatrix($-1)|double operator[] ($, int Index);
...
};
...
SparseMatrix a(10,10,10);
a[1][1][1] = ...
...
Briefly, the constructor indicates that the "level of indirection" of the
declared instance is specified by the number of arguments to the constructor.
The operator[] is specified to return the class type one level of indirection
lower than the current instance. Note that there is a special case if the
current level of indirection is the lowest. In this case, the return value is
double (or whatever type is specified).
In the current standard of C++, I have to create a "3D" matrix class which
returns a "2D" class which returns a "1D" class. In order to create 9D matrix
I would have to describe 9 different class types, all basically the same. It
seems to me that providing this type of functionality could be very useful in
many different kinds of applications.
My question is, has anything like this been suggested for C++ and if not why?
Miguel Corazao
miguelc@eecs.berkeley.edu
P.S. For those who are curious about how one could implement the sparse matrix
in this manner, the idea is that when an nD matrix is instantiated, one
instance of each of the n-1 lower levels is automatically instantiated
hierarchically within the class. All of these instances point to some common
list of data for the entire matrix.
Author: jjb@watson.ibm.com (John Barton)
Date: Mon, 28 Feb 1994 13:21:23 GMT Raw View
In article <2klsol$ap@agate.berkeley.edu>, miguelc@saguaro.eecs.berkeley.edu (Miguel Corazao) writes:
|> I am writing this article to share a grip about C++ and seek expert info as
|> to whether a solution has been proposed in for the standard.
|>
|> One major source of frustration I have had in C++ is the inability to simulate
|> repeated indirection adequately (I am told the features I am looking for exist
|> in other OO languages. I don't know the formal terminology). As an example,
|> I have created sparse matrix classes in which I tried to make the interface as
|> close to that of an array as possible. The problem is that for each level of
|> "indirection", I have to declare a new class even though all the classes are
|> the same essentially.
|>
|> Let me illustrate my point.
Let me leave out your example...
|>
|> My question is, has anything like this been suggested for C++ and if not why?
If I understand the gist of your question, then try to use templates
with integer arguments for each dimension. Projections of 9D give 8D
objects, etc., by having template for ndim have projection returning
template for ndim-1. Template specialization will halt recursion. We
have done this in the past. In our core arrays we chose to use
explicit 2D, 3D and so on to allow constructors with nD args and
subscripting with nD args. But you can do it with template parameters
if you like.
If this is not what you had in mind, you need to give a manually
created example and then the concept for abbreviating it.
|>
|>
|>
|> Miguel Corazao
|> miguelc@eecs.berkeley.edu
|>
|>
|>
|> P.S. For those who are curious about how one could implement the sparse matrix
|> in this manner, the idea is that when an nD matrix is instantiated, one
|> instance of each of the n-1 lower levels is automatically instantiated
|> hierarchically within the class. All of these instances point to some common
|> list of data for the entire matrix.
|>
--
John.
John J. Barton jjb@watson.ibm.com (914)784-6645
H1-C13 IBM Watson Research Center P.O. Box 704 Hawthorne NY 10598