不存在合适的构造函数来从“哑指针”转换为“哑指针”。到“智能指针”

发布于 2024-11-10 08:27:55 字数 361 浏览 2 评论 0原文

struct A
{
    A(int a);
};

struct B
{
    B();
    void b(std::shared_ptr<A> a);
};

int main()
{
    A a(1);
    B b;
    b.b(&a);
}

所以我收到了这个错误,抱歉,这是我第一次使用智能指针!

错误:

不存在合适的构造函数可从 "A *" 转换为 "std::tr1::shared_ptr"

我该如何解决此问题!?

struct A
{
    A(int a);
};

struct B
{
    B();
    void b(std::shared_ptr<A> a);
};

int main()
{
    A a(1);
    B b;
    b.b(&a);
}

So I got this error, sorry guys it's my frist time with the smart pointers!!

Error:

no suitable constructor exists to convert from "A *" to "std::tr1::shared_ptr<A>"

How do I fix this problem!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

王权女流氓 2024-11-17 08:27:55

智能指针的全部要点就是拥有所有权。也就是说,负责释放它所指向的任何内容。试图告诉它管理已经由完全不同的系统管理的东西根本没有意义。

在您的情况下,a 已经被自动管理,为什么您还希望 由智能指针管理?即使这有效,你也只是将自己设置为删除它两次,这就是 UB。

要么给它一些东西来拥有,比如new A(1),或者改变b来操作它不拥有的东西。

The entire point of a smart pointer is to have ownership. That is, it's responsible for the deallocation of whatever it's pointing it. It simply doesn't make sense to try to tell it to manage something that's already being managed by a completely different system.

In your case, a is already being automatically managed, why would you want to also be managed by a smart pointer? Even if that worked, you'd just be setting yourself for deleting it twice, which is UB.

Either give it something to own, like new A(1), or change b to operate on something it doesn't own.

静谧幽蓝 2024-11-17 08:27:55

其他人已经抱怨了代码的设计错误,但并不是代码无法编译的真正问题。 shared_ptr 有一个接受原始指针的构造函数,但它被标记为 explicit,这意味着您必须显式写出您想要构造一个 shared_ptr< /代码> 实例。您的函数调用尝试的是隐式地进行该构造,由于显式关键字,这是不允许的。

以下代码将编译,但会给出未定义的行为,因为 shared_ptr 将(尝试)删除驻留在堆栈上且不可删除

b.b(shared_ptr<A>(&a)); // explicit construction

的对象:< code>shared_ptr 是你可以向构造函数传递一个删除器,当应该删除拥有的指针时将调用该删除器。您可以编写并使用“noop”删除器,它什么也不做;以下不会调用未定义的行为,也不会尝试删除堆栈变量:

// outside of main
void noop_deleter(A*){/*do nothing*/}

 // call...
b.b(shared_ptr<A>(&a, noop_deleter));

如果您有一个绝对需要 shared_ptr 的库 API,但您想调用它,那么实际上这是有用途的带有堆栈变量。但该 API 的设计是另一回事......

Others already ranted on the design error of your code, but not the real problem why the code doesn't even compile. shared_ptr has a constructor that accepts a raw pointer, but it is marked as explicit, which means you have to explicitly write out that you want to construct a shared_ptr instance. What your function call tries, is to do that construction implicitly, which isn't allowed because of the explicit keyword.

The following will compile but give undefined behaviour because the shared_ptr will (try to) delete an object which resides on the stack and is as such not deleteable:

b.b(shared_ptr<A>(&a)); // explicit construction

A special trait of shared_ptr is that you can pass the constructor a deleter, which will be called when the owned pointer should be deleted. You can just write and use a "noop" deleter, which does just nothing; the following will not invoke undefined behaviour and will not try to delete the stack variable:

// outside of main
void noop_deleter(A*){/*do nothing*/}

 // call...
b.b(shared_ptr<A>(&a, noop_deleter));

And there actually is a use for this, if you have a library API that absolutely wants a shared_ptr but you want to call it with a stack variable. The design of that API is another thing though...

夏末 2024-11-17 08:27:55

std::tr1::shared_ptr 有一个构造函数,允许传递给定的原始指针。因此,如果您有一个指向 A 的指针,您会执行以下操作:

std::shared_ptr (pMyA)

但在您的情况下,指向 A 的指针指向一个自动变量,而不是指向可以在使用后删除的动态分配的内存资源。

像这样的东西将是一个更好的用例:

class B
{
  void b (shared_ptr <A> pA) {}
}

int main ()
{
  shared_ptr<A> pA (new A);
  B b;
  b.b (pA);
  ...
}

std::tr1::shared_ptr has one constructor that allows to pass down a given raw pointer. So if you had a pointer to A, you would do something like:

std::shared_ptr (pMyA)

but in your case, your pointer to A points to an automatic variable NOT to a dynamically allocated memory resource that can be deleted after usage.

Something like this would be a better use case:

class B
{
  void b (shared_ptr <A> pA) {}
}

int main ()
{
  shared_ptr<A> pA (new A);
  B b;
  b.b (pA);
  ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文