[comp.lang.c++] passing functions in C++

lear@NET.BIO.NET (Eliot Lear) (02/25/89)

I wanted arrays that would use strings as their indices.  I also
wanted a way to apply certain actions to all members of a given array.
What I would like to have is something like the following:

void MyArray::apply(int (*f)(String,String))

In English, I'd like to pass a pointer to a function.  G++ loudly
objects to the above definition.

Anyone got ideas?

Thanks in advance,
-- 
Eliot Lear
[lear@net.bio.net]

schmidt@glacier.ics.uci.edu (Doug Schmidt) (02/25/89)

In article <Feb.24.16.36.02.1989.14005@NET.BIO.NET> lear@NET.BIO.NET (Eliot Lear) writes:
++
++ I wanted arrays that would use strings as their indices.  I also
++ wanted a way to apply certain actions to all members of a given array.
++ What I would like to have is something like the following:
++
++ void MyArray::apply(int (*f)(String,String))
++
++ In English, I'd like to pass a pointer to a function.  G++ loudly
++ objects to the above definition.
++
++ Anyone got ideas?

If you change it to

  void MyArray::apply(auto int (*f)(String,String))

or use a typedef to declare the type of parameter ``f'' then G++ will
have no problems with it.  This difficulty is documented in the
g++.texinfo file and stems from g++'s use of an LALR(1) grammar to
parse the (ambiguous) C++ language.

Interestingly enough, cfront 1.2.1 also seems to have the same problem
with this particular example (properly expanded to include declaration
of class MyArray and type String, of course).

Hope this helps!

Doug
--
schmidt@ics.uci.edu (ARPA) |   Per me si va nella citta' dolente.
office: (714) 856-4043     |   Per me si va nell'eterno dolore.
                           |   Per me si va tra la perduta gente.
                           |   Lasciate ogni speranza o voi ch'entrate.