vector <unique_ptr <myclass>&gt;之间的指针分子

时间:2016-05-20 11:40:11

标签: c++ c++11 binary-tree stdvector unique-ptr

我有这堂课:

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

using namespace std;

class person
{
public:
    person(string name, string surname):_name(name), _surname(surname){}
    virtual ~person(){}
    void print()
    { cout  << _name << ' ' << _surname << endl
        << "mother: " << _mother->get_name() << ' ' << _mother->get_surname() << endl
        << "father: " << _father->get_name() << ' ' << _father->get_surname() << endl; 
    }
    string get_name(){return _name;}
    string get_surname(){return _surname;}

    void set_parents(person &mother, person &father)
    {
        _mother = unique_ptr<person>(&mother);
        _father = unique_ptr<person>(&father);
    }
private:
    string _name, _surname;
    unique_ptr<person> _mother, _father;
};

然后是主要功能:

int main()
{
    vector<unique_ptr<person> > people;
    vector<unique_ptr<person> >::iterator iter;

    people.push_back(unique_ptr<person>(new person("Marisa", "Miller")));
    people.push_back(unique_ptr<person>(new person("Andrew", "Clark")));
    people.push_back(unique_ptr<person>(new person("Thomas", "Clark")));
    people.push_back(unique_ptr<person>(new person("Elisa",  "Clark")));
    people.push_back(unique_ptr<person>(new person("Edward",  "Drake")));
    people.push_back(unique_ptr<person>(new person("Jhon",  "Drake")));

    // here is the problem:
    people.at(2).set_parents(???)

    for(iter = people.begin(); iter != people.end(); ++iter)
    {
        (*iter)->print();
    }

    return 0;
}

通过指针,我将定义以下族谱:

[Marisa Miller]     [Andrew Clark]
        |                   |
        +---------+---------+
                  |
                  +--------------[Thomas Clark]
                  |
                  +--------------[Elisa Clark]      [Edward Drake]
                                       |                   |
                                       +---------+---------+
                                                 |
                                           [Jhon Drake]

问题是: 如何将指针(_mother_father,通过get_parents(...)函数)设置为向量中包含的前一个元素? get_parents()函数也可以定义为:

void get_parents(person* mother, person* father)

void get_parents(unique_ptr<person> mother, unique_ptr<person> father)

感谢您的任何建议

2 个答案:

答案 0 :(得分:0)

有一个唯一的指针,因此指针始终只在一个地方。您有超过1个引用(在向量中,可能在Person中,如果它是父亲或母亲)。

您可能希望查看使用共享指针和弱指针: http://en.cppreference.com/w/cpp/memory/shared_ptr http://en.cppreference.com/w/cpp/memory/weak_ptr

答案 1 :(得分:0)

person不应该拥有_mother_father

这可能更好:

// ...
private:
  std::string _name, _surname;
  person *_mother;
  person *_father;
};  // class person

现在,如果你保留std::vector<std::unique_ptr<person>> people;(即容器拥有其元素),你可以写:

void person::set_parents(person &mother, person &father)
{
  _mother = &mother;
  _father = &father;
}

people[2]->set_parents(*people[0], *people[1]);
people[3]->set_parents(*people[0], *people[1]);
people[5]->set_parents(*people[3], *people[4]);

同时修复person::print成员函数(如果父项缺失则崩溃)。