Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

c++ - Typedeffing a function (NOT a function pointer)

typedef void int_void(int);

int_void is a function taking an integer and returning nothing.

My question is: can it be used "alone", without a pointer? That is, is it possible to use it as simply int_void and not int_void*?

typedef void int_void(int);
int_void test;

This code compiles. But can test be somehow used or assigned to something (without a cast)?


/* Even this does not work (error: assignment of function) */
typedef void int_void(int);
int_void test, test2;
test = test2;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What happens is that you get a shorter declaration for functions.

You can call test, but you will need an actual test() function.

You cannot assign anything to test because it is a label, essentially a constant value.

You can also use int_void to define a function pointer as Neil shows.


Example

typedef void int_void(int);

int main()
{
    int_void test; /* Forward declaration of test, equivalent to:
                    * void test(int); */
    test(5);
}

void test(int abc)
{
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...