Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ declarative syntax (http://www.velocityreviews.com/forums/t746687-c-declarative-syntax.html)

muler 04-12-2011 11:37 AM

C++ declarative syntax
 
Q: Write a function declaration that takes a double argument and
returns a pointer to a function that takes an int argument and
returns
a pointer to char without using a typedef?

using a typedef, it's easy:

typedef char* (*Ptr_func)(int);
Ptr_func Func(double);

Thanks,

SG 04-12-2011 11:51 AM

Re: C++ declarative syntax
 
On 12 Apr., 13:37, muler <mulugeta.abe...@gmail.com> wrote:
> Q: Write a function declaration that takes a double argument and
> returns a pointer to a function that takes an int argument and
> returns
> a pointer to char without using a typedef?
>
> using a typedef, it's easy:
>
> typedef char* (*Ptr_func)(int);
> Ptr_func Func(double);


char *(*Func(double))(int);

Not that I would write something like this without a typedef in real
code. Basically, you have a type on the left side -- here: char -- and
a declarator on the right side -- here: *(*Func(double))(int). You
can think of the declarator like an expression and the type infront of
the declarator spedifies the type of this expression. The only
difference between declarator and expression that comes to my mind
right now is that the unary ampersand has a different meaning. The
unary ampersand is an address operator in an expression and a
"reference declarator" in a declarator. With this understanding, you
can read the above declaration "inside out".

SG

muler 04-12-2011 12:39 PM

Re: C++ declarative syntax
 
On Apr 12, 2:51*pm, SG <s.gesem...@gmail.com> wrote:
> On 12 Apr., 13:37, muler <mulugeta.abe...@gmail.com> wrote:
>
> > Q: Write a function declaration that takes a double argument and
> > returns a pointer to a function that takes an int argument and
> > returns
> > a pointer to char without using a typedef?

>
> > using a typedef, it's easy:

>
> > typedef char* (*Ptr_func)(int);
> > Ptr_func Func(double);

>
> * char *(*Func(double))(int);
>
> Not that I would write something like this without a typedef in real
> code. Basically, you have a type on the left side -- here: char -- and
> a declarator on the right side *-- here: *(*Func(double))(int). You
> can think of the declarator like an expression and the type infront of
> the declarator spedifies the type of this expression. The only
> difference between declarator and expression that comes to my mind
> right now is that the unary ampersand has a different meaning. The
> unary ampersand is an address operator in an expression and a
> "reference declarator" in a declarator. With this understanding, you
> can read the above declaration "inside out".
>
> SG


nice!


All times are GMT. The time now is 11:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.