gpt4 book ai didi

C++ 调用未初始化的变量

转载 作者:行者123 更新时间:2023-11-28 06:47:16 24 4
gpt4 key购买 nike

我正在查看来自 v8 的以下源代码,我对 handle_scope 对象感到困惑。看起来它在初始化之前就被调用了。 documentation指定它是一个堆栈分配的对象。是否为该对象自动调用默认构造函数?

// Utility function that wraps a C++ http request object in a
// JavaScript object.
Handle<Object> JsHttpRequestProcessor::WrapMap(map<string, string>* obj) {
// Handle scope for temporary handles.
HandleScope handle_scope;

// Fetch the template for creating JavaScript map wrappers.
// It only has to be created once, which we do on demand.
if (map_template_.IsEmpty()) {
Handle<ObjectTemplate> raw_template = MakeMapTemplate();
map_template_ = Persistent<ObjectTemplate>::New(raw_template);
}
Handle<ObjectTemplate> templ = map_template_;

// Create an empty map wrapper.
Handle<Object> result = templ->NewInstance();

// Wrap the raw C++ pointer in an External so it can be referenced
// from within JavaScript.
Handle<External> map_ptr = External::New(obj);

// Store the map pointer in the JavaScript wrapper.
result->SetInternalField(0, map_ptr);

// Return the result through the current handle scope. Since each
// of these handles will go away when the handle scope is deleted
// we need to call Close to let one, the result, escape into the
// outer handle scope.
return handle_scope.Close(result);
}

最佳答案

是的,该对象是默认初始化的,这意味着将调用其默认构造函数。就像你声明一个 std::string 一样:

std::string str;

str 仍处于初始化状态。

default-initialization 表示不初始化,仅针对非类类型。

关于C++ 调用未初始化的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24737205/

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