I know Java but don’t have much knowledge of C++. I am trying to write a class for the first 3 statements in main function of the code at https://developers.google.com/v8/get_started.
First I have questions about how objects are created in C++. See the below code.
HandleScope handle_scope;
Persistent<Context> context = Context::New();
Context::Scope context_scope(context);
I think in C++ when you declare a variable of a class an instance of the class created at the point. You do not need to use new keyword like in Java. So the first statement would create an instance of HandleScope which will be stored in handle_scope.
Now I do not understand how the second statement would work. With my knowledge the part before = will create a new Persistent object which can be referred by the variable context. Then Context::New() will create a new object and store it in context? Huh, I know I am wrong. But I simply dont get how it would work?
I am trying to write a C++ class for the above. Here is my attempt.
class MyClass {
private:
HandleScope handle_scope;
Persistent<Context> context;
Context::Scope context_scope;
public:
MyClass();
};
MyClass::MyClass()
{
context = Context::New();
context_scope = new Context::Scope(context);
}
Have I done the initialization properly?
EDIT: Reply to peachykeen (in comments)
I did the following experimentation.
I wrote a Test class as below.
Test
{
public:
Test() {
cout << “Test” << endl;
}
};
In the main function I wrote Test test; It outputs “Test” which means an object is created without using new keyword.
You are right, in C++, objects are created as soon as they’re defined. You do not need to use the
newkeyword.However, unlike in Java, objects can be created with different kinds of duration. Using
newcreates an object on the heap, with dynamic storage duration: the variable lives until you explicitlydeleteit. (Andnewreturns a pointer to the created object, so that you can track it)If you simply define an object, as in your first and third lines, then it is created with automatic storage duration: that is, the object exists until it goes out of scope.
This means that you can create objects inside a function, and be guaranteed that they’ll be destroyed as soon as you leave the function — regardless of how you leave the function. Whether you return, or throw an exception, all objects with automatic storage duration (created without using
new) are guaranteed to be properly cleaned up.This means that you should always avoid
newwhenever possible. If you have to usenew, you should typically wrap the resulting pointer into a smart pointer class, an object created with automatic storage duration, so that it gets destroyed automatically). The smart pointer will then calldeleteon the new-allocated object automatically, ensuring, again, that you don’t leak memory.This distinction is a very powerful tool, which good C++ programmers need to understand well. It is a key to avoiding memory leaks, or more generally, resource leaks of all kinds, and it is, in some respects, more powerful than Java’s garbage collector.
For example, say we wish to open a file, and then write some data to it. In C++, we can do it like this:
And because
filewas declared without usingnew, because it has automatic storage duration, we are guaranteed that it will have its destructor invoked when it goes out of scope, and it will be properly cleaned up — that is, the stream will be flushed, and the file handle will be closed.It doesn’t matter if
doStuffmight throw an exception. No matter how we leavefoo,filewill be properly destroyed, so we don’t need to mess about withtry/finallylike you would in Java. The class is exception-safe by itself, without requiring any additional effort from the user.Try writing a similar snippet in Java, one which guarantees that even if
doStuffthrows an exception, the file will be immediately closed. It’ll be much longer, and requires more care on the part of the user.