欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

#define 和typedef的区别

程序员文章站 2024-03-23 11:40:40
...

源于知乎的一个帖子:
https://www.zhihu.com/question/29798061

看到一个有趣的例子,贴过来

typedef int P();
typedef int Q();
class X{
    static P(Q);   //define Q to be a P.
                   //equivalent to ''static int Q()''
                   //the parentheses arount Q are redundant

                   //Q is no longer a type in this scope
    static Q(P);   //define Q to be a function
                   //taking an argument of type p
                   //and returning an int.
                   //equivalent to ''static int Q(int())''
};

这是c++创始人写的《c++语言的设计和演化》书上的例子

static P(Q);
相当于 P Q,(类似int a)。P表示一个返回值为int的无参函数,所以这行代码的意思就是:static int Q()

static Q(P);
这个时候Q不再是一个类型,而是被定义为一个函数,表示
static int Q(int())