Topic: overloading pointers or references to functions


Author: jarausch@igpm.rwth-aachen.de (Helmut Jarausch)
Date: 29 Nov 1993 14:26:05 GMT
Raw View
Beg your pardon, if this questions has been dealt with earlier or if this
isn't the right news-group - but I think the experts can be met here.
I am just a newcomer to this group.
I would like to overload a pointer (or reference) to a function. *I* can't
see any sentence in the ARM which disallows this. So, is it senseless to
require code like to following to be OK ?
struct A
{ int i; };
struct B
{ double r; };

double geti( const A& x) { return double(x.i); }
double getd( const B& x) { return x.r; }

int main()
{ double (*F)(const A&)=geti;
  double (*F)(const B&)=getd;
}
which upto now gives a redefinition error. Such a feature would quite useful
especially if for function-reference parameter in (other) functions.
Thanks for any explanation - I just want to understand the rational behind it.
Helmut Jarausch
Institut fuer Geometrie und Praktische Mathematik
RWTH-Aachen, Germany




Author: barmar@think.com (Barry Margolin)
Date: 29 Nov 1993 22:13:09 GMT
Raw View
In article <2dd0pt$bq3@urmel.informatik.rwth-aachen.de> jarausch@igpm.rwth-aachen.de (Helmut Jarausch) writes:
>int main()
>{ double (*F)(const A&)=geti;
>  double (*F)(const B&)=getd;
>}
>which upto now gives a redefinition error. Such a feature would quite useful
>especially if for function-reference parameter in (other) functions.
>Thanks for any explanation - I just want to understand the rational behind it.

Function pointers are just variables, and C++ doesn't allow multiple
definitions of any variables.  One could just as easily ask why it doesn't
allow:

void PrintInt(int i);
void PrintFloat(float f);
...
int main() {
    int Var = 1;
    float Var = 1.0;
    PrintInt(Var); /* uses int Var */
    PrintFloat(Var); /* uses float Var */
}

Furthermore, suppose you try to assign an overloaded function to F:

double getit(const A&);
double getit(const B&);
...
    F = getit;

Which getit() should it assign to F?

You said that you think this would be useful for function-reference
parameters; I assume you mean something like:

int GetGetter(double (*&f)(const A&));

The problem occurs if GetGetter is overloaded, so there's also:

int GetGetter(double (*&f)(const B&));

Then GetGetter(F) would be ambiguous.
--
Barry Margolin
System Manager, Thinking Machines Corp.

barmar@think.com          {uunet,harvard}!think!barmar