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 开发者_JAVA百科attached to String class or the New member function in C++?
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:
- v8::String - the externally visible one (Here is doxygen documentation showing the class hierarchy).
- v8::internal::String - the internal representation.
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);
...
};
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. :)
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?
精彩评论