蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

验证 struct 点/箭头访问、值传递隔离、指针 typedef 别名与位域/访问控制

CPP本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。explore-cpp-struct.cpp
explore-cpp-struct.cpp114 行
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <type_traits>

struct LegacyBook {
    char title[50];
    char author[50];
    char subject[100];
    int book_id;
};

static int mutate_by_value(LegacyBook copy) {
    copy.book_id = 99999;
    return copy.book_id;
}

struct BookRecord {
    std::string title;
    std::string author;
    std::string subject;
    int book_id;

    BookRecord(std::string t, std::string a, std::string s, int id)
        : title(std::move(t)), author(std::move(a)), subject(std::move(s)), book_id(id) {}

    std::string summary() const {
        return title + "|" + author + "|" + subject + "|" + std::to_string(book_id);
    }
};

static std::string describe_ptr(const BookRecord* ptr) {
    return ptr->title + "#" + std::to_string(ptr->book_id);
}

static std::string describe_ref(const BookRecord& ref) {
    return ref.summary();
}

typedef struct AliasBook {
    int id;
    double price;
} AliasBook;

typedef long int* pint32;

struct CompactFlags {
    unsigned read_flag : 1;
    unsigned write_flag : 1;
    unsigned level : 3;
private:
    int secret_code = 7;
public:
    int code() const { return secret_code; }
};

int main() {
    // 1. Member access via dot operator & pass-by-value isolation
    LegacyBook b1{};
    std::strcpy(b1.title, "C++ Guide");
    std::strcpy(b1.author, "BlueSky");
    std::strcpy(b1.subject, "Systems");
    b1.book_id = 12345;
    int inside_val = mutate_by_value(b1);
    std::string act1 = std::string(b1.title) + ":" + b1.author + ":" +
                       std::to_string(b1.book_id) + ":" + std::to_string(inside_val);
    const std::string exp1 = "C++ Guide:BlueSky:12345:99999";
    bool ok1 = (act1 == exp1);

    // 2. Constructor, pointer (->), and const reference passing
    BookRecord rec1("C++ Guide", "BlueSky", "Programming", 12345);
    BookRecord rec2("CSS Guide", "BlueSky", "Frontend", 12346);
    const BookRecord* p1 = &rec1;
    std::string act2 = describe_ptr(p1) + " ; " + describe_ref(rec2);
    const std::string exp2 = "C++ Guide#12345 ; CSS Guide|BlueSky|Frontend|12346";
    bool ok2 = (act2 == exp2);

    // 3. typedef struct alias & pointer typedef applies to every declarator
    AliasBook ab{101, 59.5};
    pint32 x = nullptr, y = nullptr, z = nullptr;
    bool all_ptrs = std::is_same_v<decltype(x), long int*> &&
                    std::is_same_v<decltype(y), long int*> &&
                    std::is_same_v<decltype(z), long int*>;
    std::string act3 = std::to_string(ab.id) + ":" + (all_ptrs ? "all_ptr" : "mismatch");
    const std::string exp3 = "101:all_ptr";
    bool ok3 = (act3 == exp3);

    // 4. Bit-fields, default public members, and private access control inside struct
    CompactFlags f{};
    f.read_flag = 1;
    f.write_flag = 0;
    f.level = 5;
    std::string act4 = std::to_string(f.read_flag) + "," + std::to_string(f.write_flag) + "," +
                       std::to_string(f.level) + "," + std::to_string(f.code());
    const std::string exp4 = "1,0,5,7";
    bool ok4 = (act4 == exp4);

    std::cout << "{\n"
              << "  \"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\",\n"
              << "  \"cases\": [\n"
              << "    {\"name\": \"struct_dot_access_and_by_value\", \"expected\": \"" << exp1
              << "\", \"actual\": \"" << act1 << "\", \"passed\": " << (ok1 ? "true" : "false") << "},\n"
              << "    {\"name\": \"struct_pointer_arrow_and_const_ref\", \"expected\": \"" << exp2
              << "\", \"actual\": \"" << act2 << "\", \"passed\": " << (ok2 ? "true" : "false") << "},\n"
              << "    {\"name\": \"struct_typedef_aliases\", \"expected\": \"" << exp3
              << "\", \"actual\": \"" << act3 << "\", \"passed\": " << (ok3 ? "true" : "false") << "},\n"
              << "    {\"name\": \"struct_bitfield_and_access_control\", \"expected\": \"" << exp4
              << "\", \"actual\": \"" << act4 << "\", \"passed\": " << (ok4 ? "true" : "false") << "}\n"
              << "  ]\n"
              << "}\n";
    return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。

入口文件:projects/cplusplus-cpp-struct-lab/explore-cpp-struct.cpp

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-cpp-struct.cpp,或下载右侧课程工程包。
  2. 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:C++

课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip

下载课程关联工程包:C++

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2398.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习