Show Lecture.InheritedMethods as a slide show.
💰
class Foo { public: Foo(const string &n) : name(n) { } string name; }; Foo a("Diane"), b(a); cout << a.name << ' ' << b.name << '\n';
Diane Diane
There’s no copy ctor, so who set b.name
to Diane
?
class Foo { public: Foo(const string &n) : name(n) { } string name; }; Foo a("Diane"), b(a); cout << a.name << ' ' << b.name << '\n';
Diane Diane
There is a default copy ctor, which performs a bitwise copy on the object.
NO!
The default copy ctor & assignment operators look like this:
class Foo { public: Foo(const string &n) : name(n) { } Foo(const Foo &rhs) : name(rhs.name) { } Foo &operator=(const Foo &rhs) { name = rhs.name; return *this; } string name; }; Foo a("Diane"), b(a); b = a; cout << a.name << ' ' << b.name << '\n';
Diane Diane
class Foo { public: Foo() = default; // I love this one. Foo &Foo(const Foo &) = delete; // Not wild about this one. ... };
Great! That explains everything!
Wait—what about inheritance?
struct Base { // struct ≡ public access string name; }; struct Derived : public Base { Derived(const string &n) { name = n; } }; Derived a("Dorothy"), b(a); cout << a.name << ' ' << b.name << '\n';
Dorothy Dorothy
How did that work‽ Who copied name
? Sure, the Base
default
copy ctor would copy name
, but nobody called that.
Derived
copy ctor called the Base
copy ctor.
Base
”.
struct Base { // struct ≡ public access Base() { } Base(const Base &rhs) : name(rhs.name) { } Base &operator=(const Base &rhs) { name = rhs.name; return *this; } string name; }; struct Derived : public Base { Derived(const string &n) { name = n; } Derived(const Derived &rhs) : Base(rhs) { } Derived &operator=(const Derived &rhs) { Base::operator=(rhs); return *this; } }; Derived a("Dorothy"), b(a); cout << a.name << ' ' << b.name << '\n';
Dorothy Dorothy