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 8052395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:32:07+00:00 2026-06-05T07:32:07+00:00

I’m using the code under Constructors and destructors here Basically, i would like to

  • 0

I’m using the code under “Constructors and destructors” here

Basically, i would like to combine it with the pointer construction under
“Pointers to classes” (a little below) which i find really neat.

Now this works:

int main () {
   CRectangle * prect;     //l2
   CRectangle rect (3,4);  //l3
   prect = ▭          //l4
   cout << "rect area: " << (*prect).area() << endl;
   return 0;
}

My questions is, can we replace l2-l4 by a more elegant method
not requiring the creation of rect on line 3?

  • 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-05T07:32:08+00:00Added an answer on June 5, 2026 at 7:32 am

    To create an object without requiring any automatic variables (such as rect in your code above) you must use the new operator. Objects created with the new operator are stored in the free store, and the new expression evaluates into a pointer to the newly created object.

    Now, one could go on and say, new operator is the answer and that’s it, but it’s actually not: it does not answer the question of replacing those couple of lines with a more elegant solution, because it wouldn’t be one.

    The rest of this answer goes on a tangent of how new could be used.

    Unlike automatic objects, objects stored in the free store are not automatically destructed, but rather their life-time and destruction is controlled by the delete operator (you should delete objects you no longer need to free resources). To ensure destruction, you should always store the pointer from a new expression into what’s called a smart pointer. A good, simple rule that carries one far: use the new operator only inside a smart pointer constructor (unless you know what you’re doing).

    There are several smart pointers in C++11, while earlier versions of the standard only defined one, the auto_ptr. Perhaps due to it’s quirks, or simply because it got a replacement, it’s actually deprecated in C++11, and shouldn’t probably be used in new code, at least not in C++11 (now, this is an opinion).

    An example of smart pointer use:

    boost::shared_ptr<CRectangle> shared_rect(new CRectange(3, 4));
    std::unique_ptr<CRectangle> rect(new CRectangle(3, 4)); // C++11 only
    
    // use smart pointers like like regular pointers; indirection through * or ->
    // i.e. (*rect).area() or rect->area()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.