C语言实现类

#ifndef __DEFINE__H__
#define __DEFINE__H__

#define vector3(type) \
typedef struct vector3_##type { \
    type a; \
    type b; \
    type c; \
}vector3_##type;  // vector3##_type会报错 

#define ABS_CLASS(type)             \
typedef struct cl_##type cl_##type; \
struct cl_##type  // 没有这一行会报错 
    
vector3(int); // 用来初始化其后面的内容 

#endif

上面是"define.h"文件

#include<stdio.h>
#include"define.h"

ABS_CLASS(float){
    float a;
    float b;
    float c;
    float d;
    float (*sum)();
};

cl_float vecf4={10.0,20.0,30.0,40.0,NULL};

float sum(cl_float cl)
{
    return cl.a+cl.b+cl.c+cl.d;
}


int main()
{
    vector3_int vec;
    vec.a=10;
    vec.b=20;
    vec.c=30;
     vecf4.sum=sum;
    printf("vec.a+vec.b+vec.c=%d\n",vec.a+vec.b+vec.c); 
    printf("vecf4.sum=%f\n",vecf4.sum(vecf4)); 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/ligei/p/12520002.html