是否可以取消引用共享指针,为它分配和分配一个新对象,如下所示:
void foo()
{
std::shared_ptr<std::string> x =
std::make_shared<std::string>();
bar(*x); // is this fine?
// x == bsl::string("WHATEVER")
}
void bar(string& y)
{
y = string("whatever");
}
Run Code Online (Sandbox Code Playgroud) 给定下面的代码是否正确,编译器应生成Node()哪个应调用std::array<Node *, 100>()哪个应初始化所有指向nullptr的100指针。
旁注:我知道如果使用std::array<Node *, 100> children {};,我可以做到这一点,但我并不是想让我的代码正常工作(它已经做到了),而是试图确保它不是偶然地工作。
struct Node
{
int value;
std::array<Node *, 100> children;
}
Run Code Online (Sandbox Code Playgroud)
更新:
这里的指针是垃圾:
struct Node
{
int value;
std::array<Node *, 100> children;
}
struct Node
{
Node() : children() {}
int value;
std::array<Node *, 100> children;
}
Run Code Online (Sandbox Code Playgroud)
这里的指针是nullptr:
struct Node
{
int value;
std::array<Node *, 100> children {};
}
struct Node
{
Node() : children{} {}
int value;
std::array<Node *, 100> children;
}
Run Code Online (Sandbox Code Playgroud)
请改正我,如果我错了。