add inherit and class pointer casting
All checks were successful
continuous-integration/drone Build is passing

This commit is contained in:
jiacai 2022-09-25 23:13:27 +08:00
parent 218044c19e
commit 9e8c685653
2 changed files with 54 additions and 0 deletions

View File

@ -8,3 +8,4 @@ steps:
- cd class || exit
- g++ construct.cpp -o construct && ./construct
- g++ inherit.cpp -o inherit && ./inherit

53
class/inherit.cpp Normal file
View File

@ -0,0 +1,53 @@
#include <iostream>
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;
}