From 9e8c6856539bc36da8d32e4dcd78ec9771d703c0 Mon Sep 17 00:00:00 2001 From: jiacai Date: Sun, 25 Sep 2022 23:13:27 +0800 Subject: [PATCH] add inherit and class pointer casting --- .drone.yml | 1 + class/inherit.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 class/inherit.cpp diff --git a/.drone.yml b/.drone.yml index b7dcad8..7036cfb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,3 +8,4 @@ steps: - cd class || exit - g++ construct.cpp -o construct && ./construct + - g++ inherit.cpp -o inherit && ./inherit diff --git a/class/inherit.cpp b/class/inherit.cpp new file mode 100644 index 0000000..1e2be11 --- /dev/null +++ b/class/inherit.cpp @@ -0,0 +1,53 @@ +#include + +class Base1 { + virtual void func() {} + char ch; + // int val; +}; + +class Base2 { + virtual void func() {} + // char ch; + int val; +}; + +class Base3 { + // virtual void func() {} + char ch; + // int val; + // double val; +}; + +class Base4 { + // virtual void func() {} + // char ch; + int val; + // double val; +}; +class Derived : public Base1, public Base2, public Base3, public Base4 { + virtual void func() {} + double val; +}; + +int main() { + Derived d1; + Derived* pd1 = &d1; + Base1* pb1 = &d1; + Base2* pb2 = &d1; + Base3* pb3 = &d1; + Base4* pb4 = &d1; + + std::cout << "notice how those casts are performed according to member alignments" << std::endl; + std::cout << pd1 << std::endl + << pb1 << std::endl + << pb2 << std::endl + << pb3 << std::endl + << pb4 << std::endl; + + std::cout << sizeof(Derived) << std::endl + << sizeof(Base1) << std::endl + << sizeof(Base2) << std::endl + << sizeof(Base3) << std::endl + << sizeof(Base4) << std::endl; +} \ No newline at end of file