From c0d76f164df850f96a24351a6e00b8fa580a2429 Mon Sep 17 00:00:00 2001 From: CYP12138 <1375405618@qq.com> Date: Wed, 16 Mar 2022 18:05:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=AF=91=E5=99=A8=E4=BC=98=E5=8C=96si?= =?UTF-8?q?zeof,private=E7=BB=A7=E6=89=BF=E8=AE=BF=E9=97=AE=E6=9D=83?= =?UTF-8?q?=E9=99=90=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- basic_content/sizeof/README.md | 21 +++++++++--- basic_content/sizeof/geninhe.cpp | 14 ++++++-- .../day5/继承访问权限/readme.md | 32 ++++++++++++++++--- 3 files changed, 57 insertions(+), 10 deletions(-) diff --git a/basic_content/sizeof/README.md b/basic_content/sizeof/README.md index 7cfdbc0..4fd0e12 100644 --- a/basic_content/sizeof/README.md +++ b/basic_content/sizeof/README.md @@ -93,7 +93,7 @@ int main() /** * @file geninhe.cpp * @brief 1.普通单继承,继承就是基类+派生类自身的大小(注意字节对齐) - * 注意:类的数据成员按其声明顺序加入内存,与访问权限无关,只看声明顺序。 + * 注意:类的数据成员按其声明顺序加入内存,无访问权限无关,只看声明顺序。 * 2.虚单继承,派生类继承基类vptr * @author 光城 * @version v1 @@ -117,7 +117,14 @@ class A * int b * short a * long b - * 根据字节对齐4+4=8+8+8=24 + * 根据字节对齐4+4+8+8=24 + * + * 或编译器优化 + * char a + * short a + * int b + * long b + * 根据字节对齐2+2+4+8=16 */ class B:A { @@ -125,24 +132,30 @@ class B:A short a; long b; }; +/** +* 把A的成员拆开看,char为1,int为4,所以是1+(3)+4+1+(3)=12,()为字节补齐 +*/ class C { A a; char c; }; + class A1 { virtual void fun(){} }; -class C1:public A1 +class C1:public A { }; + int main() { cout< public + +protected -> protected + +private -> ɷ + ˽м̳ -಻ɷʻκγԱ뺯 +ҲɷʻprivateԱ + + private̳ + +public -> private + +protected -> private + +private -> ɷ ̳ ʽΪprotectedļ̳гΪ̳Уּ̳зʽ£ @@ -16,6 +32,14 @@ protectedprivateԱбԭķȨ ע㣺ñ̳еʱpublicԱΪprotectedԱʹ߲ɷʣɷʣ + protected̳ + +public -> protected + +protected -> protected + +private -> ɷ + ġԻԱķʽ 1.ֱͨӷʻԱ