String::New:这是什么?
我有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是对的。即调用
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
中,但它也被截断。您可以下载源代码并查看文件。它是这样说的:
You are correct. That is calling the
static
methodNew
on theString
class.C++ (or STL) doesn't have a native
String
class, there is astring
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 ininclude/v8.h
, but it too is truncated.You can download the source and view the files. Here is what it says:
您的解释是正确的,它是对
String
类的名为New
的静态方法的调用。但是,该
String
类不是标准的std::string
类,因为,正如您可以轻松看到的,它的大小写不同。可能它是其他库提供的 String 类,但在不知道上下文的情况下很难对它说任何其他话。附录
好吧,我发现了;您使用的 String 是 JavaScript 字符串的 C++ 表示形式,在 V8 引擎中完全使用。您可以找到其源代码 此处;我找不到任何有关它的文档,但评论很好。
顺便说一句,如果您刚刚接触 C++,您可能想从更简单的东西开始,也许不需要外部库,这样您就可以掌握 C++ 标准库。
---编辑---
好的,有人在我之前找到了它。 :)
Your interpretation is correct, it is a call to a static method called
New
of theString
class.However, that
String
class isn't the standardstd::string
class, since, as you can easily see, it differs in capitalization. Probably it's aString
class provided by some other library, but without knowing the context is hard to say anything else about it.Addendum
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. :)
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?