gpt4 book ai didi

c++ - auto 关键字和 smartpointers 一起?

转载 作者:搜寻专家 更新时间:2023-10-31 00:27:15 27 4
gpt4 key购买 nike

我可以:

auto o1 = new Content;

但不能:

std::shared_ptr<auto> o1(new Content);
std::unique_ptr<auto> o1(new Content);

我该怎么办?

最佳答案

你可以像这样使用make_shared/make_unique:

auto o1 = std::make_shared<Content>(); // Pass any arguments here as you would normally.
auto o2 = std::make_unique<Content>(); // Pass any arguments here as you would normally.

这些函数将所有参数转发给(在本例中)Content 的构造函数,您可以使用 auto 并且您只需编写一次类型。

关于c++ - auto 关键字和 smartpointers 一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49801020/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com