Connecting Tech Pros Worldwide Help | Site Map

pointer to member function (gcc)

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:30 PM
Newsgroup - Ann
Guest
 
Posts: n/a
Default pointer to member function (gcc)

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?




  #2  
Old July 19th, 2005, 05:30 PM
Gianni Mariani
Guest
 
Posts: n/a
Default 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]


  #3  
Old July 19th, 2005, 05:30 PM
Noah Roberts
Guest
 
Posts: n/a
Default 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

  #4  
Old July 19th, 2005, 05:30 PM
Jack Klein
Guest
 
Posts: n/a
Default 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
  #5  
Old July 19th, 2005, 05:30 PM
White Wolf
Guest
 
Posts: n/a
Default 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


  #6  
Old July 19th, 2005, 05:30 PM
Kevin Goodsell
Guest
 
Posts: n/a
Default 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.

  #7  
Old July 19th, 2005, 05:30 PM
Newsgroup - Ann
Guest
 
Posts: n/a
Default 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


  #8  
Old July 19th, 2005, 05:30 PM
Gianni Mariani
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.