You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++类有限读写权限实现困惑:师生类权限设计求助

C++ 实现Student与Teacher类的权限控制方案

针对你需求里的私有成员+差异化读写权限要求,可以通过两种可行方案实现,避开子类继承或全友元类的问题:

方案一:友元函数精细控制权限

通过仅开放特定成员函数为友元,而非整个类,严格限制访问范围:

#include <vector>
#include <string>

// 前向声明
class Teacher;

class Student {
private:
    std::vector<std::string> reports_; // 私有报告列表
    // 仅允许Teacher的read_student_report函数访问报告
    friend void Teacher::read_student_report(const Student&);
public:
    // Student自身的报告读写接口
    void add_report(const std::string& report) {
        reports_.push_back(report);
    }
    void remove_report(size_t index) {
        if (index < reports_.size()) {
            reports_.erase(reports_.begin() + index);
        }
    }
    // Student读取Teacher成绩的函数(需声明为Teacher的友元)
    void read_teacher_score(const Teacher& teacher);
};

class Teacher {
private:
    std::vector<int> scores_; // 私有成绩列表
    // 仅允许Student的read_teacher_score函数访问成绩
    friend void Student::read_teacher_score(const Teacher&);
public:
    // Teacher自身的成绩读写接口
    void add_score(int score) {
        scores_.push_back(score);
    }
    void update_score(size_t index, int new_score) {
        if (index < scores_.size()) {
            scores_[index] = new_score;
        }
    }
    // Teacher读取Student报告的函数
    void read_student_report(const Student& student) {
        // 仅能读取报告,无法修改
        for (const auto& report : student.reports_) {
            // 此处实现读取逻辑,比如打印
        }
    }
};

// 实现Student读取Teacher成绩的函数
void Student::read_teacher_score(const Teacher& teacher) {
    // 仅能读取成绩,无法修改
    for (int score : teacher.scores_) {
        // 此处实现读取逻辑,比如打印
    }
}

方案特点

  • 严格控制访问权限:仅开放指定函数访问私有成员,避免全友元类带来的过度权限
  • 代码简洁直接,适合小型项目场景

方案二:接口类实现只读访问

通过定义只读接口类,让Student和Teacher分别实现对应接口,依赖接口而非具体类,降低耦合:

#include <vector>
#include <string>
#include <iostream>

// 报告只读接口
class IReportReader {
public:
    virtual const std::vector<std::string>& get_reports() const = 0;
    virtual ~IReportReader() = default;
};

// 成绩只读接口
class IScoreReader {
public:
    virtual const std::vector<int>& get_scores() const = 0;
    virtual ~IScoreReader() = default;
};

class Student : public IReportReader {
private:
    std::vector<std::string> reports_;
public:
    // Student自身的报告读写权限
    void add_report(const std::string& report) {
        reports_.push_back(report);
    }
    void remove_report(size_t idx) {
        if (idx < reports_.size()) {
            reports_.erase(reports_.begin() + idx);
        }
    }
    // 实现报告只读接口,返回const引用确保无法修改
    const std::vector<std::string>& get_reports() const override {
        return reports_;
    }
    // 通过IScoreReader接口读取Teacher成绩
    void read_teacher_scores(const IScoreReader& teacher) {
        const auto& scores = teacher.get_scores();
        for (int s : scores) {
            std::cout << "读取教师成绩:" << s << std::endl;
        }
    }
};

class Teacher : public IScoreReader {
private:
    std::vector<int> scores_;
public:
    // Teacher自身的成绩读写权限
    void add_score(int score) {
        scores_.push_back(score);
    }
    void update_score(size_t idx, int new_score) {
        if (idx < scores_.size()) {
            scores_[idx] = new_score;
        }
    }
    // 实现成绩只读接口,返回const引用确保无法修改
    const std::vector<int>& get_scores() const override {
        return scores_;
    }
    // 通过IReportReader接口读取Student报告
    void read_student_reports(const IReportReader& student) {
        const auto& reports = student.get_reports();
        for (const auto& r : reports) {
            std::cout << "读取学生报告:" << r << std::endl;
        }
    }
};

方案特点

  • 符合面向对象设计原则,耦合度低,扩展性强(后续可新增其他角色通过接口访问)
  • 无需使用友元,完全依赖接口实现权限控制

内容的提问来源于stack exchange,提问作者Sarah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 15:58:17