網站首頁 編程語言 正文
struct 結構體
占用空間
在C++ 中空結構體占1個字節
struct Temp{};sizeof(Temp); // 1struct Temp{}; sizeof(Temp); // 1
1.字節對齊規則(為了便于cpu尋址) 元素的首地址必須是當前元素所占空間的整數倍
2.結構體的總長度必須是最大元素所占空間的整數倍
struct Stu{ int age; char sex; } sizeof(Stu); // 8 最后補三個字節滿足規則2 struct Stu2{ char sex; int age; } sizeof(Stu2); // 8 char后面補三個字節滿足規則1
與類的區別
訪問控制權限
結構體在C++ 中的默認訪問控制權限都是***public***的,而class的默認是***private***的
主要用途
結構體主要是作為數據的集合
而類是對象,是屬性和方法的集合
c結構體實現虛函數的功能
#include <stdio.h> typedef int(*fun)(void); int getParentSex() { return 1; } int getSonSex() { return 2; } struct Parent { fun getSex; }; struct Son { fun getSex; }; int main() { // 通過函數指針的賦值達到虛函數的作用 struct Parent parent; parent.getSex = getParentSex; struct Son son; son.getSex = getSonSex; printf("Parent sex is %d \nson sex is %d\n", parent.getSex(), son.getSex()); getchar(); }
總結
原文鏈接:https://blog.csdn.net/guuci/article/details/122973706
相關推薦
- 2022-03-10 Winform自定義控件在界面拖動、滾動鼠標時閃爍的解決方法_C#教程
- 2022-08-15 前端提交代碼時使用ESLint進行規范校驗報錯(Git husky > pre-commit(nod
- 2023-07-26 webpakc原理之開發一個清除console.log(xxx)的loader
- 2022-07-03 C#列表List<T>、HashSet和只讀集合介紹_C#教程
- 2022-12-15 C++?Boost?Thread線程使用示例詳解_C 語言
- 2022-02-24 JDBC中在結果集中以列順序獲取值時注意類型匹配
- 2022-05-01 React的三大屬性你都知道嗎_React
- 2022-04-18 Mac安裝 nvm 之后,node is not command 每次都要重新nvm use版本
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支