Re: int (*&)()
On Feb 8, 12:58 pm, Asif Zaidi <asifnza...@gmail.com> wrote:
On Feb 8, 12:28 am, Syron <mr.sy...@googlemail.com> wrote:
Am 08.02.2010 04:45, schrieb Asif Zaidi:
What does the syntax in subject line mean - how should I
read it.
if this is a typedef (like 'int(*&func)()') it is a
reference to a pointer to a function that returns an int.
I am trying to implement the following and getting compile
failure. Any suggestions ?
typedef int (*& func_ptr1)();
int goo()
{
cout << "un goo" << endl;
return 0;
}
int main()
{
func_ptr1 p1 = goo;
}
Why? The formal reason why it doesn't work is that you're
trying to initialize a reference to a non-const with an rvalue.
But the real question is "what do you think you're trying to
do?" To make the above work, it's sufficient to change the
typedef to:
typedef int (*const & func_ptr1)();
But while the results may be legal C++, they still don't make
any logical sense.
The error is below
1>d:\profiles\waz003\my documents\visual studio 2008\projects
\hw5_3\hw5_3\hw5_3.cpp(174) : error C2440: 'initializing' : cannot
convert from 'int (__cdecl *)(void)' to 'func_ptr1'
The error message could be better, the fact remains: you can't
initialize a reference to a non-const with an rvalue. Given,
however, that at least in the above case, it doesn't make any
sense to even want to, the question remains: what are you really
trying to do?
--
James Kanze