| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"George2" |
| Date: |
19 Jan 2008 07:56:06 AM |
| Object: |
why the code can compile -- about function pointer |
Hello everyone,
I am learning code from others, but I do not know why the following
code section can compile?
Why assignment p = new (void (*[3])()) is ok? Are the left side and
right side of assignment having compatible type?
[Code]
void (**p)();
int main()
{
p = new (void (*[3])());
return 0;
}
[/Code]
thanks in advance,
George
.
|
|
| User: "Bo Persson" |
|
| Title: Re: why the code can compile -- about function pointer |
19 Jan 2008 02:20:58 PM |
|
|
George2 wrote:
Hello everyone,
I am learning code from others, but I do not know why the following
code section can compile?
Why assignment p = new (void (*[3])()) is ok? Are the left side and
right side of assignment having compatible type?
[Code]
void (**p)();
int main()
{
p = new (void (*[3])());
return 0;
}
[/Code]
Why bother?
In real code you don't want to do this, ever!
Bo Persson
.
|
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: why the code can compile -- about function pointer |
19 Jan 2008 09:26:51 AM |
|
|
George2 wrote:
Hello everyone,
I am learning code from others, but I do not know why the following
code section can compile?
Why assignment p = new (void (*[3])()) is ok? Are the left side and
right side of assignment having compatible type?
Not just compatible. They have the very same type.
[Code]
void (**p)();
So it's a pointer to pointer to function taking no parameters and returning
void.
int main()
{
p = new (void (*[3])());
Here you create an array of 3 pointers to functions taking no parameters and
returning void. The operator new returns a pointer to the first element of
that array, which happens to have the same type as p.
return 0;
}
[/Code]
A simpler example that is similar in that regard would be one that creates
an array of int instead of an array of pointers to functions:
int *p;
int main()
{
p = new int[3];
return 0;
}
.
|
|
|
|

|
Related Articles |
|
|