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

时间:2014-07-14 13:05:23

标签: c++ v8

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

// 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);
}

1 个答案:

答案 0 :(得分:1)

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

一样
std::string str;

str尚未初始化。

只有非类类型的默认初始化才意味着没有初始化。