简体   繁体   English

如何使用包含内部类的类的实例有效地访问内部类的成员?

[英]How can I efficiently access members from inner classes using instances of a class which includes inner classes?

I want to design a structure using 5 particular classes:Person,Driver,Employee,Child and Parent.我想设计一个使用 5 个特定类的结构:Person、Driver、Employee、Child 和 Parent。

-Every driver is a person. - 每个司机都是一个人。

-Every employee is both a driver and a person. -每位员工既是司机又是人。

-Every child is a person. - 每个孩子都是一个人。

-Every parent is a person,driver,employee and it can have one or more children. - 每个父母都是一个人,一个司机,一个雇员,可以有一个或多个孩子。

Here's what I had in mind:这是我的想法:

class Parent {
public:
    class Employee {
    public:
        class Driver {
        public:
            class Person {
                string name;
                int age;
            public:
                string GetName() { return name; }
                void SetName(string name) { this->name = name; }
                int GetAge() { return age; }
                void SetAge(int age) { this->age = age; }
            };
        private:
            Person person;
            string carName;
        public:
            Person GetPerson() { return person;}
            void SetPerson(Person person) { this->person = person;}
            string GetCarName() { return carName; }
            void SetCarName(string carName) { this->carName = carName;}
        };
    private:
        Driver driver;
    public:
        Driver GetDriver() { return driver; }
        void SetDriver(Driver driver) { this->driver = driver; }
    };
    class Child {
    public:
        class Person:public Employee::Driver::Person {
        };
    private:
        Person person;
        string nameOfSchool;
    public:
        Person GetPerson() { return person; }
        void SetPerson(Person person) { this->person = person;}
        string GetNameOfSchool(){ return nameOfSchool;}
        void SetNameOfSchool(string nameOfSchool) { this->nameOfSchool = nameOfSchool;}
    };
private:
    Employee employee;
    Child child;
public:
    Employee GetEmployee() { return employee; }
    void SetEmployee(Employee employee) { this->employee = employee;}
    Child GetChild() { return child;}
    void SetChild(Child child) { this->child = child;}
};

But when I try something like:但是当我尝试类似的东西时:

Parent random_parent;
    random_parent.GetEmployee().GetDriver().GetPerson().SetName("Joseph");
    random_parent.GetEmployee().GetDriver().GetPerson().SetAge(80);
    cout << random_parent.GetEmployee().GetDriver().GetPerson().GetName() << endl << random_parent.GetEmployee().GetDriver().GetPerson().GetAge();

I get just this garbage value:我只得到这个垃圾值:

-858993460

How can I make any instance of Parent work and be able to access and initialize the name and age from inner class Person ?如何使Parent任何实例工作并能够从内部类Person访问和初始化nameage

Design-wise, driver, employee, child and parent are not descendants of a Person.在设计方面,驱动程序、员工、孩子和父母都不是 Person 的后代。 They are rather roles and one Person can have any number of roles.它们是角色,一个人可以有任意数量的角色。 Or, they can be relationships between 2 persons, where, for example, a person is child to one person and a parent to another.或者,它们可以是两个人之间的关系,例如,一个人是一个人的孩子,而另一个人是父母。

GetPerson , GetDriver , GetChild and GetEmployee should return either references or pointers. GetPersonGetDriverGetChildGetEmployee应该返回引用或指针。 Right now, when you call random_parent.GetEmployee() , it returns a brand new, temporary, Employee object which is a copy of the one in random_parent .现在,当您调用random_parent.GetEmployee() ,它会返回一个全新的临时Employee对象,它是random_parent对象的副本。 If you do random_parent.GetEmployee().SetDriver(new_driver) , it sets the driver in this brand new Employee object, not the one in random_parent .如果您执行random_parent.GetEmployee().SetDriver(new_driver) ,它会在这个全新的Employee对象中设置驱动程序,而不是random_parent的驱动程序。 The temporary Employee object is then discarded after the statement is executed.然后在执行语句后丢弃临时Employee对象。

If you change如果你改变

Employee GetEmployee() { return employee; }

to

//     here
//      |
//      V
Employee& GetEmployee() { return employee; }

then random_parent.GetEmployee() will return a reference to the employee object in random_parent .然后random_parent.GetEmployee()将返回random_parent employee对象的random_parent random_parent.GetEmployee().SetDriver(new_driver); will update that object, which is what you expected to happen.将更新该对象,这是您期望发生的事情。

Do the same with GetDriver , GetPerson , and GetChild .GetDriverGetPersonGetChild执行相同的操作。


This fixes your immediate problem.这解决了您的直接问题。 However, your code is poorly designed.但是,您的代码设计得很差。 You could get design advice on Code Review .您可以在Code Review上获得设计建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM