问题描述
我有类 B
,它包含一个指向类 A
的对象的指针。我希望他们分享他们的立场,所以我使用了 std::shared_ptr
。以下是此代码的简短版本:
struct A {
std::shared_ptr<int> pos;
A(const A& right,std::shared_ptr<int> _pos = nullptr) :
pos(_pos == nullptr ? (std::make_shared<int>()) : _pos) { }
};
struct B {
std::shared_ptr<int> pos;
A* a;
B(const B& right) :
pos(std::make_shared<int>()),a(new A(*right.a,pos)) { }
};
在实际程序中它似乎工作正常,但是 Visual Studio 在 C26815 for dangling pointer
复制构造函数中给出警告 B
,当给 a
赋值时。从 pos
中删除 B
不会影响警告,但如果它也从 A
中删除,警告就会消失,所以我猜它与 shared_ptr
有关系吗?这里有悬空的指针,还是错误的警告?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)