Connecting Tech Pros Worldwide Forums | Help | Site Map

pointer to member function (gcc)

Newsgroup - Ann
Guest
 
Posts: n/a
#1: Jul 19 '05
I have the following code:

class nn_base {
public:
double phi(double v)
{
return v;
};
double ComputeY(int i, int j, double (nn_base::*activeFunc)(double))
{
return (this->*activeFunc)(1.0);
}
};

main()
{
nn_base nn1;
double x = nn1.ComputeY(1, 2, nn_base::phi);
return 0;
}

It can be compiled by Visual C++ both 6.0 and .NET but cannot by gcc 3.22,
which complains:


test7.cpp: In function `int main()':
test7.cpp:18: no matching function for call to `nn_base::ComputeY(int, int,
<unknown type>)'
test7.cpp:10: candidates are: double nn_base::ComputeY(int, int, double
(nn_base::*)(double))

what's wrong with that?




Gianni Mariani
Guest
 
Posts: n/a
#2: Jul 19 '05

re: pointer to member function (gcc)


Newsgroup - Ann wrote:[color=blue]
> I have the following code:
>
> class nn_base {
> public:
> double phi(double v)
> {
> return v;
> };
> double ComputeY(int i, int j, double (nn_base::*activeFunc)(double))
> {
> return (this->*activeFunc)(1.0);
> }
> };
>
> main()[/color]

should be

int main()
[color=blue]
> {
> nn_base nn1;
> double x = nn1.ComputeY(1, 2, nn_base::phi);[/color]

should be:

double x = nn1.ComputeY(1, 2, & nn_base::phi);

actually, I'm not sure about needing the "&" but it seems like it makes
more sense if it is needed.

VC++ likes the "&".
[color=blue]
> return 0;
> }
>
> It can be compiled by Visual C++ both 6.0 and .NET but cannot by gcc 3.22,
> which complains:
>[/color]


Noah Roberts
Guest
 
Posts: n/a
#3: Jul 19 '05

re: pointer to member function (gcc)


Gianni Mariani wrote:[color=blue][color=green]
>> main()[/color]
>
>
> should be
>
> int main()[/color]


In C any function that is declaired without a return type is defaulted
to return int. Therefore "main() {...}" is acceptable (though commonly
thought of as bad form) in C. Is this different in C++?

NR

Jack Klein
Guest
 
Posts: n/a
#4: Jul 19 '05

re: pointer to member function (gcc)


On Sun, 14 Sep 2003 12:25:24 -0700, Noah Roberts
<nroberts@dontemailme.com> wrote in comp.lang.c++:
[color=blue]
> Gianni Mariani wrote:[color=green][color=darkred]
> >> main()[/color]
> >
> >
> > should be
> >
> > int main()[/color]
>
>
> In C any function that is declaired without a return type is defaulted
> to return int. Therefore "main() {...}" is acceptable (though commonly
> thought of as bad form) in C. Is this different in C++?
>
> NR[/color]

Implicit int is illegal in C++, and it has been illegal in C since the
1999 major update to the C language standard.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
White Wolf
Guest
 
Posts: n/a
#5: Jul 19 '05

re: pointer to member function (gcc)


Noah Roberts wrote:[color=blue]
> Gianni Mariani wrote:[color=green][color=darkred]
>>> main()[/color]
>>
>>
>> should be
>>
>> int main()[/color]
>
>
> In C any function that is declaired without a return type is defaulted
> to return int. Therefore "main() {...}" is acceptable (though
> commonly thought of as bad form) in C. Is this different in C++?[/color]

Yes. And as far as I know in C as well. C (today) is C99 and C99 does not
allow implicit int anymore. So main withhout a return type is illegal in
both C and C++.

--
WW aka Attila


Kevin Goodsell
Guest
 
Posts: n/a
#6: Jul 19 '05

re: pointer to member function (gcc)


Gianni Mariani wrote:
[color=blue]
>
> should be:
>
> double x = nn1.ComputeY(1, 2, & nn_base::phi);
>
> actually, I'm not sure about needing the "&" but it seems like it makes
> more sense if it is needed.[/color]

I believe it is needed in the case of a member function, but not in the
case of a non-member function.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Newsgroup - Ann
Guest
 
Posts: n/a
#7: Jul 19 '05

re: pointer to member function (gcc)



"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:%P39b.3452$BS5.2932@newsread4.news.pas.earthl ink.net...[color=blue]
> Gianni Mariani wrote:
>[color=green]
> >
> > should be:
> >
> > double x = nn1.ComputeY(1, 2, & nn_base::phi);
> >
> > actually, I'm not sure about needing the "&" but it seems like it makes
> > more sense if it is needed.[/color]
>
> I believe it is needed in the case of a member function, but not in the
> case of a non-member function.
>
> -Kevin
> --
> My email address is valid, but changes periodically.
> To contact me please use the address from a recent posting.
>[/color]

Good, thank you all. Could anybody tell me why is the difference? Thanks.

-Ann


Gianni Mariani
Guest
 
Posts: n/a
#8: Jul 19 '05

re: pointer to member function (gcc)


Noah Roberts wrote:[color=blue]
> Gianni Mariani wrote:
>[color=green][color=darkred]
>>> main()[/color]
>>
>>
>>
>> should be
>>
>> int main()[/color]
>
>
>
> In C any function that is declaired without a return type is defaulted
> to return int. Therefore "main() {...}" is acceptable (though commonly
> thought of as bad form) in C. Is this different in C++?[/color]

This is different in C++.

Only constructors and destructors may be declared without return types.

void is a valid return type indicating that nothing may be returned.

Closed Thread