add inherit and class pointer casting
All checks were successful
continuous-integration/drone Build is passing
All checks were successful
continuous-integration/drone Build is passing
This commit is contained in:
parent
218044c19e
commit
9e8c685653
@ -8,3 +8,4 @@ steps:
|
|||||||
|
|
||||||
- cd class || exit
|
- cd class || exit
|
||||||
- g++ construct.cpp -o construct && ./construct
|
- g++ construct.cpp -o construct && ./construct
|
||||||
|
- g++ inherit.cpp -o inherit && ./inherit
|
||||||
|
53
class/inherit.cpp
Normal file
53
class/inherit.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user