我们经常遇到这样的情况, 类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;
}