-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctors.cpp
More file actions
59 lines (42 loc) · 1.65 KB
/
Copy pathctors.cpp
File metadata and controls
59 lines (42 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#pragma clang diagnostic ignored "-Wunused-variable"
#pragma clang diagnostic ignored "-Wunused-value"
#define CATCH_CONFIG_MAIN // Tells Catch2 to provide a main()
#include "../catch/catch_amalgamated.hpp"
#include "utils.h"
using namespace std;
namespace {
// ctors
// -
//
// Key notes:
// member initialized always executes completely before the ctor body runs.
// Exact order of execution when an object is created:
// 1. Base class ctors are initialized first (in the order they are inherited).
// 2. Non-static member variables are init. in the order they are declared in
// the class definition.
// 3. Ctor body executes last.
//
struct Foo {
int x, y;
Foo() : x(0), y(0) {} // default ctor
Foo(int x, int y) : x(x), y(y) {} // parameterized ctor
explicit Foo(int x) : x(x), y(0) {} // explicit — no implicit conversion
Foo(const Foo& o) : x(o.x), y(o.y) {} // copy ctor
// copy assign. operator (not ctor, the obj already exists)
Foo& operator=(const Foo& o) {
x = std::move(o.x); y = std::move(o.y); return *this;
}
// Note: returns Foo& to enable chaining:
// e.g. for `a = b = c`, `b = c` returns b&, then `a = b` uses that
// reference; w/o the reference return, chaining would be impossible
Foo(Foo&& o) : x(std::move(o.x)), y(std::move(o.y)) {} // move ctor
Foo& operator=(Foo&& o) { x = o.x; y = o.y; return *this; } // move assign.
~Foo() {} // destructor
};
TEST_CASE("ctor-1") {
Foo foo1, foo2;
Foo foo3 = foo1; // copy ctor
Foo foo4 { foo1 }; // copy ctor
foo3 = foo2; // copy assign.
}
}