Hello all:

I would appreciate some advice on array subscripts.

I am having problems passing 2-D arrays to functions I have written. The reason is that some of the arrays are dynamic arrays subscripted as array[j], whereas some of them were created in a class I wrote and these are subscripted as array(i,j).

I know that I could change my class to use array[j] subscripting, but it would be very helpful to be able to pass arrays that have BOTH types of subscripting to my functions.

Can anyone suggest how to do this? I hope I have made sense--I'm pretty new to this.

Recommended Answers

All 4 Replies

Operators are just functions, the reason you cannot pass () and [] as the same argument is that they are not necessarily the same function, they have different memory addresses and associated methodology.

Rather, why not just overload your function?

void foo( int a ) { ; }
void foo( short a ) { ; }

Thanks for your help, Unimportant.

Well, I did consider that, but I was hoping that it would be simpler to figure out a way of using [][] and (,)...

My function is a template function. Would I overload it by defining TWO versions of the function--one that uses [][] and the other that uses (,) subscripting? Sorry, this is probably a silly question.

template <class T>
T& foo(T &t) {
 // ;
}
template <class T>
T& foo(T &t, T &t2) {
 // ;
}

You may or may not be familiar with function pointers, but I'm going to use one as an example:

int foo(int x) { return x; }
int foo_caller(int (*foo)(int), int r) { return (*foo)(r); } // you can do fun stuff with void pointers here
int main() {
    void (*fptr)(int);
    fptr = foo;
    fptr(1);
    (*fptr)(2);
    foo_caller(fptr,x);
    return 0;
}

Now picture your compilers problem:
You want to pass a totally different kind of function.

I probably am about to go off on too much of a tangent, so I'm going cut it short.

Hope this was informative!

I didn't know about function pointers, but I see what you're doing. Thanks! This is very helpful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.