What is new in C + +?

Source: Internet
Author: User

"New" is a keyword in C + + that is typically used to create an object, but unlike the general method of creating an object, it returns a pointer to the object. When we use the keyword new to create an object dynamically on the heap, it actually does three things: get a chunk of memory, call the constructor, and return the correct pointer.

As we have defined the following class A:

Class A
{
   int i;
Public:
   A (int _i): I (_i*_i) {}
   void Say ()  {printf ("i=%d/n", I);}
};
Call NEW:
* pa = new A (3);
So the process of creating an object above is roughly equivalent to the following three sentences (in general): a
* pa = (*) malloc (sizeof (A) *3);
PA->A::A (3);
Return PA;

The point is to understand that when new creates an object it returns the pointer to the image, returns the pointer to the image, and returns the pointer to the image. Important thing to say three times.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.