C++ 共享指针的向量更新push_back上的现有元素

C++ 共享指针的向量更新push_back上的现有元素,c++,vector,shared-ptr,C++,Vector,Shared Ptr,基本上,我有一个shared_ptr向量作为类的成员数据。 通过从另一个类函数调用成员函数来填充此向量。 问题添加到向量的新元素会更新现有元素的值 设置如下所示: A类 /* * Class A has a header with class declarations and a distinct implementation. * For the sake of brevity, the relevant information is shown in this snippet. */

基本上,我有一个
shared_ptr
向量作为类的成员数据。 通过从另一个类函数调用成员函数来填充此向量。
问题添加到向量的新元素会更新现有元素的值

设置如下所示:

A类

/*
 * Class A has a header with class declarations and a distinct implementation.
 * For the sake of brevity, the relevant information is shown in this snippet.
 */
class A {
public:
    A(std::string name) : m_name(name);
    std::string& get_name();
private:
    std::string m_name;
}
typedef std::shared_ptr<A> A_ptr;
class C
class B
调用
add_element()
,并不断向所述向量添加元素。
据我所知,问题可能在于我在该循环中使用了共享指针。

但我不知道我到底做错了什么。

如果你发布了一篇文章,我相信有人能给出一个明确的答案,但一个好的经验法则是不要将智能指针放在标准容器中。这些集装箱设计用于处理惰性货物,而智能指针更像是挑剔的乘客。我在某处有一个列表,其中包含类型应该遵守的规则,如果我能找到它的话…无法复制@Beta谢谢你的建议。我很想得到这份清单,也许是一份github回购协议(或者一个简单的要点),当你找到它的时候?不过,当这些惰性货物一旦超出范围就会被摧毁时,你怎么把它们放进集装箱里?@Sopel,这很奇怪。我将再次查看完整的代码,看看是否遗漏了相关信息。@Beta是的,根据您的建议,我花时间复习了基础知识,并意识到我没有必要使用共享指针。事实上,最基本的部分是在使用指针之前问问自己是否需要指针。现在我有了一个唯一的指针(它实际上是必需的,这样值就可以在函数终止后继续存在)。以前,我没有充分的理由将指针分散在各处。如果可能的话,我会写一个关于我改变了什么的答案,让其他人看看它是否对他们有效。再次感谢您的洞察力。
/*
 * Class B has a header with class declarations and a distinct implementation.
 * For the sake of brevity, the relevant information is shown in this snippet.
 */
class B {
public:
    void add_element(A_ptr& element) {
        /*
         this is where the problem is:
         i DO get the proper element (i can verify this by calling the get_name() method on `element`).

         Assume we got in succession:
         - ONE
         - TWO
         - THREE

         ...
        */
        std::cout << element -> get_name << std::endl;

        m_elements.push_back(element);

        /*
         ...

         fast forward here, when I print the content of the vector held in this class,
         the old elements added have been updated somehow and I get this:

         - when we get ONE, the vector contains: [ONE]
         - when we get TWO on the next call to this function, the vector contains: [TWO, TWO]. I expect [ONE,TWO]
         - when we get THREE on the next call to this function, the vector contains: [THREE, THREE, THREE]. I expect [ONE, TWO, THREE]

         -> WHAT ON EARTH IS GOING ON HERE?
        */
        for (auto it = m_elements.begin(); it != m_elements.end(); ++it)
            std::cout << (* it) -> get_name() << std::endl;
    }
private:
    std::vector<A_ptr> m_elements;
}
typedef std::shared_ptr<B> B_ptr;
/*
 * Class C has a header with class declarations and a distinct implementation.
 * For the sake of brevity, the relevant information is shown in this snippet.
 */
class C {
public:
    void do_something() {
        std::string name = name_obtained_dynamically();
        B_ptr manager(new B());

        do {
            A_ptr element_one(nullptr);
            A_ptr element_two(nullptr);

            if (we_are_good) {
                element_one.reset(new A(name));
            }
            else {
                element_two.reset(new A(name));
            }

            if (element_one !=nullptr)
                manager -> add_element(element_one);
            else
                manager -> add_element(element_two);
        } while(condition_is_true);
    }
}