在 Mac 上的 Xcode 中使用 libc++(GNU++11 方言)、LLVM 编译器。
我有一个结构如下:
struct Vertex
{
float position[3];
float colour[4];
float normal[3];
};
Run Code Online (Sandbox Code Playgroud)
目前我正在堆上创建一个实例,如下所示:
Vertex *vertex = new Vertex({{0.1f, 0.1f, 0.1f},
{0.0f, 0.0f, 0.0f, 0.0f},
{1.0f, 0.0f, 0.0f}} );
Run Code Online (Sandbox Code Playgroud)
不过我想创建一个共享指针。下面的代码是最好的方法吗?
Vertex v = Vertex( {{0.1f, 0.1f, 0.1f},
{0.0f, 0.0f, 0.0f, 0.0f},
{1.0f, 0.0f, 0.0f}} );
std::shared_ptr<Vertex> vertex = std::make_shared<Vertex>(v);
Run Code Online (Sandbox Code Playgroud)
或者我应该在创建共享指针之前创建Vertex
using ?new
更新
我尝试过以下方法:
auto vertex = std::make_shared<PCNVertex>( {{0.1f, 0.1f, 0.1f},
{0.0f, 0.0f, 0.0f, 0.0f},
{1.0f, 0.0f, 0.0f}} );
Run Code Online (Sandbox Code Playgroud)
按照建议,但我收到错误no matching …
我有一个名为的对象Thing
,它的构造函数接受一个int.
此代码按预期工作:
Thing thing(5);
然而,偶然我写了以下内容:
Thing thing = Thing(5); // note: no 'new'
并得到了错误no matching constructor for initialization of 'Thing'
.后一个代码的实际含义是什么?我知道如果我扔new
在那里意味着什么,但没有新的,这是什么意思?