C++ Library Extensions 2022.12.09
To help learn modern C++ programming
26-dynamic_polymorphism.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6class BaseWithVD // base with a virtual destructor
7{
8 public:
9
10 virtual ~BaseWithVD() = default;
11};
12
14{
15 public:
16
17 ~BaseWithoutVD() = default;
18};
19
20class A: public BaseWithVD
21{
22 public:
23
24 // we have not defined virtual destructor for A
25 // but, since BaseWithVA has a virtual destructor
26 // all inherited classes also have a virtual destructor
27 // So, class A gets a virtual destructor by default
28};
29
30class B: public BaseWithoutVD
31{
32 public:
33};
34
36{
37 // std::is_base_of_v<>
38 stream << "Is A a base of B ? "
39 << std::is_base_of_v<A, B> << endl;
40
41 stream << "Is BaseWithVD a base of A ? "
42 << std::is_base_of_v<BaseWithVD, A> << endl;
43
44 stream << "Is A a base of BaseWithVD ? "
45 << std::is_base_of_v<A, BaseWithVD> << endl;
46
47 stream << "Is BaseWithoutVD a base of B ?"
48 << std::is_base_of_v<BaseWithoutVD, B> << endl;
49
50 stream << "Is B a base of BaseWithoutVD ?"
51 << std::is_base_of_v<B, BaseWithoutVD> << endl;
52
53 stream << "Has A a virtual destructor ? "
54 << std::has_virtual_destructor_v<A> << endl;
55
56 stream << "Has B a virtual destructor ? "
57 << std::has_virtual_destructor_v<B> << endl;
58}
59
60int main()
61{
63}
tpf::sstream stream
void test_virtual_destructor()
virtual ~BaseWithVD()=default
~BaseWithoutVD()=default
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.