String::New:这是什么?

发布于 2024-09-06 07:26:04 字数 252 浏览 4 评论 0原文

我有Java背景,正在学习C++。我遇到了以下 C++ 代码:

String source = String::New("'Hello' + ', World'"); 

据我所知,这应该是对“String”类的静态成员函数“New”的调用。但是,我搜索了定义“String”的整个头文件,在 String 类或其超类中没有任何名为“New”的静态成员。 C++ 中的 String 类或 New 成员函数有什么特殊含义吗?

I am from a Java background and is learning C++. I encountered the following C++ code:

String source = String::New("'Hello' + ', World'"); 

As what I understand so far, this should be a call to static member function 'New' of class 'String'. But, I've searched through the whole header file defining 'String', there is not any static member named 'New' in the String class or its super classes. Is there any special meaning attached to String class or the New member function in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄风 2024-09-13 07:26:04

你是对的。即调用 String 类上的 static 方法 New

C++(或 STL)没有原生的 String 类,有一个 string 类,但它没有 >::新方法。您必须确保您正在阅读正确的文档:)

它可能是从基类继承的,因此请确保检查 String 是否是继承层次结构的一部分。

这是 v8 的 String 的处理。这很有趣。

有两种实现:

浏览 内部字符串源代码String是实际上是一个代表 JavaScript 字符串的堆分配对象。

事实证明,Google Code 的 UI 已损坏(也许它们有最大字符数?)。 v8::internal::HeapObject 源代码 应该位于 src/objects.h 中,但该文件已被截断。外部可见的 v8::String 源代码 应该位于 include/v8.h 中,但它也被截断。

您可以下载源代码并查看文件。它是这样说的:

/**
 * A JavaScript string value (ECMA-262, 4.3.17).
 */
class V8EXPORT String : public Primitive {
 public:
   ...

 /**
   * Allocates a new string from either utf-8 encoded or ascii data.
   * The second parameter 'length' gives the buffer length.
   * If the data is utf-8 encoded, the caller must
   * be careful to supply the length parameter.
   * If it is not given, the function calls
   * 'strlen' to determine the buffer length, it might be
   * wrong if 'data' contains a null character.
   */
  static Local<String> New(const char* data, int length = -1);

  /** Allocates a new string from utf16 data.*/
  static Local<String> New(const uint16_t* data, int length = -1);

  ...
};

You are correct. That is calling the static method New on the String class.

C++ (or STL) doesn't have a native String class, there is a string class, but it doesn't have a ::New method. You'll have to make sure you're reading the right documentation :)

It's possible that it's inherited from a base-class, so make sure you check if String is part of an inheritance hierarchy.

Here's the deal with v8's String. It's interesting.

There are two implementations:

Browsing the internal String source code, String is indeed a heap allocated object representing a Javascript string.

It turns out that Google Code's UI is broken (maybe they have a maximum character count?). The v8::internal::HeapObject source code should be in src/objects.h, but the file is truncated. And the externally visible v8::String source code should be in include/v8.h, but it too is truncated.

You can download the source and view the files. Here is what it says:

/**
 * A JavaScript string value (ECMA-262, 4.3.17).
 */
class V8EXPORT String : public Primitive {
 public:
   ...

 /**
   * Allocates a new string from either utf-8 encoded or ascii data.
   * The second parameter 'length' gives the buffer length.
   * If the data is utf-8 encoded, the caller must
   * be careful to supply the length parameter.
   * If it is not given, the function calls
   * 'strlen' to determine the buffer length, it might be
   * wrong if 'data' contains a null character.
   */
  static Local<String> New(const char* data, int length = -1);

  /** Allocates a new string from utf16 data.*/
  static Local<String> New(const uint16_t* data, int length = -1);

  ...
};
撩心不撩汉 2024-09-13 07:26:04

您的解释是正确的,它是对 String 类的名为 New 的静态方法的调用。

但是,该 String 类不是标准的 std::string 类,因为,正如您可以轻松看到的,它的大小写不同。可能它是其他库提供的 String 类,但在不知道上下文的情况下很难对它说任何其他话。


附录

顺便说一句,它是 google 提供的 v8 Javascript 引擎

好吧,我发现了;您使用的 String 是 JavaScript 字符串的 C++ 表示形式,在 V8 引擎中完全使用。您可以找到其源代码 此处;我找不到任何有关它的文档,但评论很好。

顺便说一句,如果您刚刚接触 C++,您可能想从更简单的东西开始,也许不需要外部库,这样您就可以掌握 C++ 标准库。

---编辑---

好的,有人在我之前找到了它。 :)

Your interpretation is correct, it is a call to a static method called New of the String class.

However, that String class isn't the standard std::string class, since, as you can easily see, it differs in capitalization. Probably it's a String class provided by some other library, but without knowing the context is hard to say anything else about it.


Addendum

btw, it is the v8 Javascript engine provided by google

Ok, I found out; that String that you are using is the C++ representation of a JavaScript string, that is throughly used in the V8 engine. You can find its source code here; I couldn't find any documentation about it, but it's well commented.

By the way, if you are just approaching C++ you may want to start with something simpler, maybe without external libraries, so you can get the grasp on the C++ standard library.

---EDIT---

Ok, someone else found it before me. :)

雨后咖啡店 2024-09-13 07:26:04

std::string 类(注意小写)是 C++ 标准库中的一个类,在头文件 中定义。在 C++ 中,new(注意小写)是一个分配内存的运算符。您需要明确的第一件事是 - 您要询问哪个字符串类?

The class std::string (note lowercase) is a class in the C++ standard library, defined in the header file <string>. In C++ new (note lowercase) is an operator that allocates memory. The first thing you need to be clear about is - which string class are you asking about?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文