Menu
CoddyTech

Classes and Shared Identity

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

C++ class variables can hold independent objects directly, so class syntax alone does not imply reference semantics. This source deliberately uses shared_ptr to share one Document instance between aliases. Swift classes always provide reference identity, with === testing whether references identify the same instance. Equal field contents do not imply that identity. A const shared_ptr does not itself make its pointee const; similarly a Swift let class reference may change instance state through allowed methods or properties. Swift classes need appropriate initializers rather than automatically receiving every struct-style memberwise initializer. Choose storage and ownership explicitly when translating object models.

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 Document { std::string title; };

int main() {
auto document = std::make_shared<Document>(Document{"draft"});
    auto alias = document;
    alias->title = "published";
    auto other = std::make_shared<Document>(Document{"published"});
    std::cout << document->title << '\n';
    std::cout << (document == other ? "same" : "different") << '\n';
}

Swift

Complete program file: Document.swift.

final class Document { var title: String; init(title: String) { self.title = title } }

Complete program file: main.swift.

let document = Document(title: "draft")
let alias = document
alias.title = "published"
let other = Document(title: "published")
print(document.title)
print(document === other ? "same" : "different")
challenge icon

Challenge

Easy

Implement Counter, a reference type with init(_ value: Int), read-only-to-callers value, and increment() adding one. Input is initial value -100 through 100 then count 0 through 20. Alias a counter, increment through the alias count times, and output the original value.

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 Counter.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 a = Counter(5); let alias = a; let separate = Counter(5); alias.increment(); alias.increment(); return a === alias && a.value == 7 && separate.value == 5
}
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