Menu
CoddyTech

Structs and Independent Copies

Lesson 26 of 48 in Coddy's Swift for C++ Developers course.

C++ default memberwise copying and Swift struct assignment preserve the semantics of their individual fields. A std::string member copies its value, while a shared_ptr member retains shared ownership of the same pointee. Swift String values and class-reference fields have a comparable distinction in this example. Changing the copied label leaves the original label alone, but changing the shared cell is visible from both copies. C++ supports custom copy operations, so this is not a universal rule for every class. Swift methods that mutate a struct require mutating; modifying a referenced class instance does not replace the containing struct field.

C++

Complete program file: main.cpp.

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <optional>
#include <memory>
#include <tuple>
#include <variant>
#include <stdexcept>
#include <algorithm>
struct Cell { std::string text; };
struct LabelledBox { std::string label; std::shared_ptr<Cell> shared; };

int main() {
LabelledBox original{"old", std::make_shared<Cell>(Cell{"one"})};
    auto copy = original;
    copy.label = "new";
    copy.shared->text = "two";
    std::cout << original.label << ' ' << original.shared->text << '\n';
}

Swift

Complete program file: Cell.swift.

final class Cell { var text: String; init(text: String) { self.text = text } }

Complete program file: LabelledBox.swift.

struct LabelledBox { var label: String; var shared: Cell }

Complete program file: main.swift.

let original = LabelledBox(label: "old", shared: Cell(text: "one"))
var copy = original
copy.label = "new"
copy.shared.text = "two"
print("\(original.label) \(original.shared.text)")
challenge icon

Challenge

Easy

Implement value type Score with stored points: Int, init(points:), and mutating award(_ amount: Int) adding amount. Input is initial points and award, each 0 through 1000. Copy the original, award only the copy, and output original and copy points separated by a space.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Score.swift, Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
let original = Score(points: 13); var copy = original; copy.award(6); copy.award(2); return original.points == 13 && copy.points == 21
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Swift for C++ Developers

Practice on your own: Swift playground