首页 > 其他分享 >typedef struct and struct

typedef struct and struct

时间:2023-08-26 18:56:21浏览次数:31  
标签:typedef struct int myStruct C++ myVariable

typedef struct and struct

status: 更新中

warning: 初学者写的内容,可能有内容上的错误

https://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c

typedef

typedef 仅仅是给已有的类型取一个别名;

typedef int my_int

typedef vs. #define

详细:https://www.cnblogs.com/kion/p/17659279.html

  • #define is a preprocessor token: the compiler itself will never see it.
  • typedef is a compier token: the preprocessor does not care about it.

C vs. C++

C

在 C(注意不是 C++ )中,像这样声明一个结构体:

struct myStruct{
    int cat;
    int dog;
};

那么声明一个实例的时候就必须要带上 struct 关键字:

struct myStruct myVariable;

所以为了偷懒有些人就会写成如下格式:

typedef struct myStruct{
    int cat;
    int dog;
} myStruct;

就会让编译器把 myStruct 替换成 struct myStruct

但是这样不一定是好的,也有说法是要尽量避免这种写法:

https://www.kernel.org/doc/html/latest/process/coding-style.html#typedefs

  • The typedef saves a little typing, but it hides the fact that it's a structure type. :会隐藏这个对象是一个结构体类型的事实;
  • 除非你确定这是需要隐藏的;

C++

在 C++ 中,可以像这样声明一个结构体:

struct myStruct{
    int cat;
    int dog;
};

struct 关键字是可选的:

myStruct myVariable; // OK
struct myStruct myVariable; // also OK!

在 C 和 C++ 中其实 structclass 的区别是:

  • struct 默认所有成员都是 public 的;
  • class 默认所有成员都是 private 的;

标签:typedef,struct,int,myStruct,C++,myVariable
From: https://www.cnblogs.com/kion/p/17659281.html

相关文章

  • Autofac报错No constructors on type 'xxx' can be found with the constructor finde
    Noconstructorsontype'JK.TitanData.Repository.Fact_Sales_GMVTargetTotalRepository'canbefoundwiththeconstructorfinder'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'.Seehttps://autofac.rtfd.io/help/no-construct......
  • 【Oracle RAC Database】Oracle Grid Infrastructure 启动流程与日志
    OS启动OHASD(OracleHighAvailabilityServices)init.ohasd.run被启动,该进程负责启动ohasd.bin守护进程[root@node01~]#ps-ef|grepohasd|grep-vgreproot5151018:59?00:00:00/bin/sh/etc/init.d/init.ohasdrun>/dev/null2>&1</dev/......
  • 代码简洁之道:对象转换神器MapStruct
    在我们日常开发的程序中,为了各层之间解耦,一般会定义不同的对象用来在不同层之间传递数据,比如xxxDTO、xxxVO、xxxQO,当在不同层之间传输数据时,不可避免地经常需要将这些对象进行相互转换。今天给大家介绍一个对象转换工具MapStruct,代码简洁安全、性能高,强烈推荐。MapStruct简介MapSt......
  • SpringBoot复习(54)用于事务处理的InfrastructureAdvisorAutoProxyCreator BeanPostProc
    从类的继承关系看InfrastructureAdvisorAutoProxyCreator是一个BeanPostProcessor.@EnableTransactionManagement注解导入了TransactionManagementConfigurationSelector类,它的代码如下:这个ImportSelector的selectImports方法返回了一个AutoProxyRegistrar,AutoProxyRegistrar代码......
  • 【论文阅读】Self-Alignment with Instruction Backtranslation自对齐与指令反翻译
     Self-AlignmentwithInstructionBacktranslation自对齐与指令反翻译摘要:在当今的人工智能时代,语言模型的训练和优化已成为研究的热点。本文介绍了一种创新且可扩展的方法,通过为人编写的文本自动标注相应的指令,构建高质量的指令跟随语言模型。此研究的方法,被命名为“指令反......
  • 结构体Struct、联合体Union与类Class
    结构体Struct、联合体Union与类Class1.Struct/Classstruct能包含成员函数吗?能!struct能继承吗?能!!struct能实现多态吗?能!!!1.1最本质的区别是默认的访问控制;结构体的继承是public的,class的继承是private的;两者也可以交叉继承,继承权限由子类决定,如结构体B继承类A,则为public继......
  • Golang之数据库转换结构体工具table2struct
    另外一个根据json生成对应结构体在线工具: https://mholt.github.io/json-to-go/ 安装:gogetgithub.com/gohouse/converter或者下载对应平台的二进制文件https://github.com/gohouse/converter/releases 引入该包进行转换的使用方式可以参考github上的使用示例,为......
  • C++ Constructor And Destructor
    ifyouhaveproblemswithconstructorsanddestructors,youcaninsertsuchprintstatementsinconstructorsforyourrealclassestoseethattheyworkasintended.Forlargerprograms,thisexactkindoftracingbecomestedious,butsimilartechniquesa......
  • org.mapstruct系列文章汇总
     Mapstruct使用教程  MapStruct:将多个源字段映射到一个目标字段   JAVA字段相同对象之间转换工具推荐   org.mapstruct.Mapper-JavaBean互相转换使用指南&&【踩坑专栏】mapstruct无法生成实现类   ......
  • 快速解决 const 与 typedef 类型组合时 ,const修饰谁的问题
    C++使用typedef给复合类型定义别名时,与const结合会产生看似“令人困惑”的类型推定,例如typedefchar*pstring;constpstringcstr=0;constpstring*ps;cstr到底是什么类型?如果直接把pstring展开成char*,就会认为cstr是constchar*类型,从而认为cstr是一个指向const......