PAYATHON 2026

std::shared_ptr 嵌套成员关系为何未指向原对象

数据小陈

结论

std::shared_ptr 本身没有问题。根因在 addTemplate 里的这行代码:

std::shared_ptr<Note> note = std::make_shared<Note>(*this);

这行用 *this 复制构造了一个新的 Note 对象,并让 templatee->note 指向这个副本。原 note 对象的 templates 里确实加入了一个 Template,但副本的 templates 还是空的。因此 templatee->note->templates.size() 为 0,检查失败。Template 的反向指针应指向当前 Note 对象本身;这样 templatee->note 才会和外部 note 是同一个对象。

原因

std::make_shared<Note>(*this) 会调用 Note 的复制构造函数,产生一个新对象。之后 templates.push_back(templatee) 里的 templates 属于当前对象,而 templatee->note 指向新副本。两者不是同一个对象,templatee->note->templates 自然看不到刚 push 的元素。

解决步骤

  1. 如果继续用 std::shared_ptr<Note> 作为反向指针,让 Note 继承 std::enable_shared_from_this<Note>。
  2. 在 addTemplate 中用 shared_from_this() 取得当前对象的 shared_ptr,赋给 templatee->note。
  3. 把 templatee push 到当前对象的 templates。
  4. 检查时比较 templatee->note 和 note 是否指向同一个对象。

代码示例:最小修复

#include <vector>
#include <memory>

class Note;
class Template {
public:
    std::shared_ptr<Note> note;
};

class Note : public std::enable_shared_from_this<Note> {
public:
    std::vector<std::shared_ptr<Template>> templates;
    void addTemplate(std::shared_ptr<Template> templatee) {
        templatee->note = shared_from_this();
        templates.push_back(templatee);
    }
};

int check(std::shared_ptr<Note> note, std::shared_ptr<Template> templatee) {
    return templatee->note->templates.size() != 1;
}

int main() {
    std::shared_ptr<Note> note = std::make_shared<Note>();
    std::shared_ptr<Template> templatee = std::make_shared<Template>();
    note->addTemplate(templatee);
    return check(note, templatee);
}

这个版本里,shared_from_this() 返回的 shared_ptr<Note> 与 main 中的 note 指向同一个对象。check 中 templatee->note->templates.size() 为 1,成功时返回 0。

shared_from_this() 只能在该对象已经由 std::shared_ptr 管理后调用。不要把它放在构造函数里,也不要对栈对象或由其他方式管理的对象调用。这里的 note 由 std::make_shared<Note>() 创建,满足这个前提。

更推荐的做法:反向关系用 weak_ptr

上面的最小修复能解决同一性问题,但 Note 持有 shared_ptr<Template>,Template 又持有 shared_ptr<Note>,会形成循环引用,引用计数无法归零。长期运行的程序中,反向关系通常用 std::weak_ptr 表示:

#include <vector>
#include <memory>

class Note;
class Template {
public:
    std::weak_ptr<Note> note;
};

class Note : public std::enable_shared_from_this<Note> {
public:
    std::vector<std::shared_ptr<Template>> templates;
    void addTemplate(std::shared_ptr<Template> templatee) {
        templatee->note = shared_from_this();
        templates.push_back(templatee);
    }
};

int check(const std::shared_ptr<Note>& note, const std::shared_ptr<Template>& templatee) {
    auto locked = templatee->note.lock();
    return (locked != note) || (note->templates.size() != 1);
}

int main() {
    auto note = std::make_shared<Note>();
    auto templatee = std::make_shared<Template>();
    note->addTemplate(templatee);
    return check(note, templatee);
}

这里 Template::note 是 std::weak_ptr<Note>,不会增加 Note 的引用计数。检查时先用 lock() 取得临时 shared_ptr,再和外部 note 比较。成功时 locked == note 且 note->templates.size() == 1,check 返回 0。

注意事项

  • 不要用 std::shared_ptr<Note>(this) 临时包一层 this。这可能与已有的 shared_ptr 形成双重所有权,导致未定义行为。
  • std::enable_shared_from_this 要求对象由 std::shared_ptr 管理。如果在构造函数里或对象尚未托管时调用 shared_from_this(),会抛异常或行为不符合预期(C++17 起通常抛 std::bad_weak_ptr)。
  • 如果业务上 Note 是聚合根并拥有多个 Template,Note -> Template 可以用 shared_ptr,Template -> Note 更适合用 weak_ptr。
  • 如果必须保留双向 shared_ptr,就要从设计上打破循环引用,或者只在生命周期很短、可以接受不释放的场景中使用。不要把它当作默认做法。

备注:内容仅供参考。