#define 和typedef的区别

来源:互联网 发布:最优化计算方法专业 编辑:程序博客网 时间:2024/06/05 11:39

源于知乎的一个帖子:
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())

原创粉丝点击