|
1 | 1 | There are several ways of constructing objects in C++, I shall summarize some of them. |
2 | 2 |
|
| 3 | +## Const and Refs |
| 4 | +Const data types and references cannot be assigned once declared. If the class uses a `const` or a reference they must be initialized using the initializer list. |
| 5 | +```cpp |
| 6 | +class CustomMap { |
| 7 | +private: |
| 8 | + const int M; |
| 9 | +public: |
| 10 | + CustomMap(int modulo) : M(modulo) {} |
| 11 | +}; |
| 12 | + |
| 13 | +int main() { |
| 14 | + CustomMap map(5); |
| 15 | + return 0; |
| 16 | +} |
| 17 | +``` |
| 18 | +
|
| 19 | +## Brace initialization |
| 20 | +This is different from constructor initializer list. Use `std::initializer_list<T>` to assign arrays. |
| 21 | +
|
| 22 | +```cpp |
| 23 | +class Graph { |
| 24 | + vector<array<int, 2>> edges; |
| 25 | +
|
| 26 | +public: |
| 27 | + // Constructor that accepts { ... } initializer |
| 28 | + Graph(initializer_list<array<int, 2>> edgeList) : edges(edgeList) { |
| 29 | + cout << "Initializer-list constructor called" << endl; |
| 30 | + } |
| 31 | +
|
| 32 | + void showEdges() const { |
| 33 | + for (int x : edges) cout << x << ""; |
| 34 | + cout << endl; |
| 35 | + } |
| 36 | +}; |
| 37 | +
|
| 38 | +int main() { |
| 39 | + Graph g = {{1, 2}, {2, 3}, {3, 1}}; // uses initializer_list ctor |
| 40 | + return 0; |
| 41 | +} |
| 42 | +
|
| 43 | +``` |
| 44 | + |
| 45 | +All the other ways are summarized in the below snippet |
| 46 | +```cpp |
| 47 | +class Order { |
| 48 | +private: |
| 49 | + int qty; |
| 50 | + int prc; |
| 51 | + char side; // buy or sell |
| 52 | + char symbol[6]; // ticker symbol name |
| 53 | + |
| 54 | +public: |
| 55 | + // Paramterized constructor |
| 56 | + Order(int q, int p, char s, const char* sym) |
| 57 | + : qty(q), prc(p), side(s) { |
| 58 | + strncpy(symbol, sym, 5); |
| 59 | + symbol[5] = '\0'; // ensure null-terminated |
| 60 | + cout << "Parameterized constructor called" << endl; |
| 61 | + } |
| 62 | + |
| 63 | + // Copy constructor |
| 64 | + Order(const Order& order) |
| 65 | + : qty(order.qty), prc(order.prc), side(order.side) { |
| 66 | + strcpy(symbol, order.symbol); |
| 67 | + cout << "Copy constructor called" << endl; |
| 68 | + } |
| 69 | + |
| 70 | + // Move constructor |
| 71 | + Order(Order&& order) noexcept |
| 72 | + : qty(order.qty), prc(order.prc), side(order.side) { |
| 73 | + strcpy(symbol, order.symbol); |
| 74 | + |
| 75 | + // Reset source |
| 76 | + order.qty = 0; |
| 77 | + order.prc = 0; |
| 78 | + order.side = '\0'; |
| 79 | + order.symbol[0] = '\0'; |
| 80 | + |
| 81 | + cout << "Move constructor called" << endl; |
| 82 | + } |
| 83 | + |
| 84 | + void displayTick() { |
| 85 | + cout << symbol << "\n"; |
| 86 | + } |
| 87 | +}; |
| 88 | + |
| 89 | +int main() { |
| 90 | + Order appleOrder(100, 50, 'B', "AAPL"); |
| 91 | + Order appleOrderCopy = appleOrder; // copy |
| 92 | + |
| 93 | + Order appleOrderMove = std::move(appleOrder); // move |
| 94 | + appleOrderMove.displayTick(); |
| 95 | + appleOrder.displayTick(); // after move |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
| 99 | +``` |
| 100 | +
|
3 | 101 |
|
0 commit comments