用boost::shared_ptr 进行数据管理

我们经常遇到这样的情况, 类A和类B同时需要使用类C这个数据。那么类C这个数据由谁来存储呢?

比如:有个Student A, Class C(班级), Student Union SU(学生会), 这个学生A既可能是班级C的一员,也可能是学生会SU的一员。

下面是一个demo。

#include <string>
#include <vector>
#include <boost/shared_ptr.hpp>
#include <iostream>
class Student
{
public:
    ~Student()
    {
        std::cout << m_strName << " Destroyed." << std::endl;
    }
    void setName(const std::string& strName)
    {
        m_strName = strName;
    }
    const std::string& getName() const
    {
        return m_strName;
    }
    void setNo(int nNo)
    {
        m_nNo = nNo;
    }
    int getNo() const
    {
        return m_nNo;
    }
    bool operator=(const Student& student) const
    {
        return m_nNo == student.m_nNo;
    }

    friend std::ostream &operator<<(std::ostream& os, const Student&s);
private:
    int m_nNo;
    std::string m_strName;
};
std::ostream& operator<<(std::ostream& os, const Student&s)
{
    os << s.m_nNo << " " << s.m_strName;
    return os;
}
typedef boost::shared_ptr<Student> StudentPtr;
typedef boost::shared_ptr<const Student> const_StudentPtr;
class StudentUnion
{
public:
    ~StudentUnion()
    {
        std::cout << m_strName << " Destroyed." << std::endl;
    }
    void setName(const std::string& strName)
    {
        m_strName = strName;
    }
    const std::string& getName() const
    {
        return m_strName;
    }
    void addMembers(StudentPtr student)
    {
        m_vecStudents.push_back(student);
    }

    const_StudentPtr FindStudent(int nNo) const
    {
        for (auto it = m_vecStudents.begin(); it != m_vecStudents.end(); it++) {
            const_StudentPtr s = *it;
            if (s->getNo() == nNo) {
                return s;
            }
        }

        return const_StudentPtr(const_StudentPtr());
    }
private:
    friend std::ostream &operator<<(std::ostream& os, const StudentUnion &stUnion);
    std::string m_strName;
    std::vector<StudentPtr> m_vecStudents;
};
typedef boost::shared_ptr<StudentUnion> StudentUnionPtr;
typedef boost::shared_ptr<const StudentUnion> const_StudentUnionPtr;

std::ostream &operator<<(std::ostream& os, const StudentUnion &stUnion)
{
    os << stUnion.m_strName << " Members: " << std::endl;
    for (auto it : stUnion.m_vecStudents) {
        std::cout << *it << std::endl;
    }
    return os;
}
class Class
{
public:
    ~Class()
    {
        std::cout << m_strName << " Destroyed." << std::endl;
    }
    void setName(const std::string& strName)
    {
        m_strName = strName;
    }
    const std::string& getName() const
    {
        return m_strName;
    }
    void addStudent(StudentPtr student)
    {
        m_vecStudents.push_back(student);
    }
    const_StudentPtr FindStudent(int nNo) const
    {
        for (auto it : m_vecStudents) {
            if (it->getNo() == nNo) {
                return it;
            }
        }

        return const_StudentPtr(const_StudentPtr());
    }
private:
    friend std::ostream &operator<<(std::ostream& os, const Class &cls);
    std::string m_strName;
    std::vector<StudentPtr> m_vecStudents;
};
typedef boost::shared_ptr<Class> ClassPtr;
typedef boost::shared_ptr<const Class> const_ClassPtr;

std::ostream &operator<<(std::ostream& os, const Class &cls)
{
    os << cls.m_strName << " Members: " << std::endl;
    for (auto it : cls.m_vecStudents) {
        std::cout << *it << std::endl;
    }
    return os;
}
int main()
{
    StudentPtr s1(new Student);
    s1->setNo(10001);
    s1->setName("huyanjie");

    StudentPtr s2(new Student);
    s2->setNo(10002);
    s2->setName("zhangsan");

    StudentPtr s3(new Student);
    s3->setNo(10003);
    s3->setName("lisi");

    std::cout << "create students:" << std::endl;
    std::cout << *s1 << std::endl;
    std::cout << *s2 << std::endl;
    std::cout << *s3 << std::endl;

    ClassPtr cls(new Class);
    cls->setName("Class 3");
    cls->addStudent(s1);
    cls->addStudent(s2);
    cls->addStudent(s3);
    std::cout << *cls << std::endl;

    StudentUnionPtr stUnion(new StudentUnion);
    stUnion->setName("Football Sports");
    stUnion->addMembers(s1);
    stUnion->addMembers(s3);
    std::cout << *stUnion << std::endl;

    {
        const_StudentPtr s = cls->FindStudent(10001);
        if (s != NULL) {
            std::cout << "Find " << s->getName() << ":" << std::endl;
            std::cout << cls->getName() << " " << *s << std::endl;
        } else {
            std::cout << "Could not Find 10001 in " << cls->getName() << std::endl;
        }
    }
    {
        const_StudentPtr s = cls->FindStudent(10002);
        if (s != NULL) {
            std::cout << "Find " << s->getName() << ":" << std::endl;
            std::cout << cls->getName() << " " << *s << std::endl;
        } else {
            std::cout << "Could not Find 10002 in " << cls->getName() << std::endl;
        }
    }

    {
        const_StudentPtr s = stUnion->FindStudent(10001);
        if (s != NULL) {
            std::cout << "Find " << s->getName() << ":" << std::endl;
            std::cout << stUnion->getName() << " " << *s << std::endl;
        } else {
            std::cout << "Could not Find 10001 in " << stUnion->getName() << std::endl;
        }
    }
 {
        const_StudentPtr s = stUnion->FindStudent(10002);
        if (s != NULL) {
            std::cout << "Find " << s->getName() << ":" << std::endl;
            std::cout << stUnion->getName() << " " << *s << std::endl;
        } else {
            std::cout << "Could not Find 10002 in " << stUnion->getName() << std::endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值