From 88118d727819b4d42c0e1f8b742e80b517b21939 Mon Sep 17 00:00:00 2001 From: Kamil Kaminski Date: Fri, 28 Feb 2014 08:31:36 -0600 Subject: demo explicit need of calling copy ctor --- inheritance_ctor_calls.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++ virtual_destructor.cpp | 40 ++++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 inheritance_ctor_calls.cpp create mode 100644 virtual_destructor.cpp diff --git a/inheritance_ctor_calls.cpp b/inheritance_ctor_calls.cpp new file mode 100644 index 0000000..6d692a8 --- /dev/null +++ b/inheritance_ctor_calls.cpp @@ -0,0 +1,77 @@ +/* g++ -Wall -std=gnu++98 -pedantic -MD -O0 -g inheritance_ctor_calls.cpp -o inheritance_ctor_calls + * + * Note: line 64 is very IMPORTANT! + * output: + * + * node1 default ctor called + * node2 default ctor called with y + * node1 default ctor called + * node2 copy ctor called + * + */ + +#include + +class Node1 +{ + public: + Node1() : x_(0) { + std::cout << "node1 default ctor called\n"; + }; + Node1(int x) : x_(x) { + std::cout << "node1 default ctor called with x\n"; + }; + + virtual void foo() { + std::cout << "node1 foo called\n"; + } + + int x_; +}; + +class Node2 : public Node1 +{ + public: + Node2() : y_(0) { + std::cout << "node2 default ctor called\n"; + }; + Node2(int y) : y_(y) { + std::cout << "node2 default ctor called with y\n"; + }; + + Node2(const Node2 &n) : y_(0) { + std::cout << "node2 copy ctor called\n"; + } + + virtual void foo() { + std::cout << "node2 foo called\n"; + /* invoke parent method */ + Node1::foo(); + /* do things unique to this class */ + /* ... */ + } + + int y_; +}; + +class Collection : public Node2 +{ + public: + /* only immediate base class may be initialized, Node1 is 2 level deep here hence error */ + Collection() : /* Node1(2), error */ Node2(1), z(0) { + } + /* parent copy ctor needs to be invoked since derived class' copy ctor is overridden */ + Collection(const Collection& c) : Node2(c), z(0) { + /* ... */ + } + + int z; +}; + +int main(int argc, char **argv) +{ + Collection c; + Collection d(c); + + return 0; +} diff --git a/virtual_destructor.cpp b/virtual_destructor.cpp new file mode 100644 index 0000000..75324c6 --- /dev/null +++ b/virtual_destructor.cpp @@ -0,0 +1,40 @@ +/* outputs: + * + * derived destructor called + * base destructor called + */ + +#include + +/* will generate a class's default ctor, copy ctor, copy assignment operator, + * by default data member and dtor are private, default ctor, copy ctor and + * assignment operator are public + */ +class Base +{ + public: + virtual ~Base(); +}; +Base::~Base() { + std::cout << "base destructor called\n"; +} + +class Derived : public Base +{ + public: + ~Derived(); +}; +Derived::~Derived() { + std::cout << "derived destructor called\n"; +} + +int main(int argc, char **argv) +{ + /* when d goes out of scope, it will call its destructor will call base + * class destructor + */ + Derived d; + Base b = d; /* conversion of d into Base takes place on rhs, d is sliced down */ + + return 0; +} -- cgit v1.2.3