I have class B
, which contains a pointer for object of class A
. I want them to share their position, so I used std::shared_ptr
for it. Below is a short version of this code:
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)) { }
};
In the actual program it seems to work fine, however Visual Studio gives warning C26815 for dangling pointer
in the B
copy constructor, when giving value to a
. Removing pos
from B
doesn't affect the warning, but if it's also removed from A
the warning disappears, so I guess it have something to do with shared_ptr
? Is there a dangling pointer here, or it's a false warning?