Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:18:20+00:00 2026-06-08T14:18:20+00:00

I know Java but don’t have much knowledge of C++. I am trying to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:18:27+00:00Added an answer on June 8, 2026 at 2:18 pm

    You are right, in C++, objects are created as soon as they’re defined. You do not need to use the new keyword.

    However, unlike in Java, objects can be created with different kinds of duration. Using new creates an object on the heap, with dynamic storage duration: the variable lives until you explicitly delete it. (And new returns 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 new whenever possible. If you have to use new, 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 call delete on 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:

    void foo() {
        std::ofstream file("foo.txt");
        doStuff(file); // call a function which does something with the file   
    }
    

    And because file was declared without using new, 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 doStuff might throw an exception. No matter how we leave foo, file will be properly destroyed, so we don’t need to mess about with try/finally like 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 doStuff throws an exception, the file will be immediately closed. It’ll be much longer, and requires more care on the part of the user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the knowledge of C++ template, but don't know Java. Would someone explain
I am testing a java project but i don't know about java console. Have
I know that Java have its own garbage collection, but sometimes I want to
I Know there's a Vector class in java, but it seems to be just
I know basics of UML and java's OO interpretations using class diagrams. But after
I want to find memory leaks in my java application but I don't know
I want to use reference in Java but I don't know how! for example
i know virtually no java but i'm trying to learn some for this project.
Okay, so I have an assignment to code (using Java,but I don't think that
I know core Java and have worked a little with J2EE. But I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.