一般有两种不同的方法
1、采用构造函数与静态构造变量结合的方法实现
首先在类中定义一个静态成员变量,然后在构造函数里面打印该静态变量的值,并对静态变量进行自增操作
#include<stdio.h>
struct print
{
static int a;
print()
{
printf("%d\n",print::a);
a++;
}
};
int print::a=1;
int main()
{
print tt[1000];
return 0;
}
2、通过使用宏定义实现
#include<stdio.h>
#define B P,P,P,P,P,P,P,P,P,P
#define P L,L,L,L,L,L,L,L,L,L
#define L I,I,I,I,I,I,I,I,I,I,N
#define I printf("%d\n",i++)
#define N printf("\n")
int main()
{
int i=1;
B;
return 0;
}
//更简便的写法如下:
#include<stdio.h>
#define A(x) x;x;x;x;x;x;x;x;x;x;
int main()
{
int n=1;
A(A(A(printf("%d",n++))));
return 0;
}