why the code can compile -- about function pointer



 DEVELOP > c-Plus-Plus > why the code can compile -- about function pointer

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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;
}
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER