C + + supplements (10) class with dynamic memory allocation (2)

Source: Internet
Author: User

Static member functions

Contains the keyword static when declared. Note the following two points:

1. A static member function cannot be invoked by an object or this pointer, but only by using the class name + scope resolver.

2. Static member functions can only use static members.

New operator

When using

#include <new>

The new operator can then specify the memory location when allocating memory. For example:

1 //Create a 512-byte memory buffer2 Char*buffer =New Char[ +];3ClassName *PTR1, *ptr2;4 5 //create a Class object in the buffer buffers6PTR1 =New(buffer) ClassName;7 //create a Class object in the heap8PTR2 =NewClassName;9 Ten Deleteptr1; One Delete[] buffer;

Delete ptr1 calls the destructor for the class, and delete [] buffer removes only the buffer buffers, not the destructor of the calling class. A viable option at this time is to show the call destructor before releasing buffer .

Nesting

C + + allows structs, classes, and enumeration definitions to be included in a class.

The scope of these types is the entire class.

This declaration does not create a data object , but it specifies that the type can be used in the scope of the class.

Member initialization syntax

1, can only be used for constructors.

2. A non-static const member must be initialized this way.

3. Reference data members must be initialized like this.

The format is as follows:

Classname::classname (int N): Mem1 (n), mem2 (0), mem3 (null) {  //  Construct ....      }

After the colon, mem1,mem2,mem3 are data members that are classname, and the order of initialization is consistent with their order in the class declaration.

C + + supplements (10) class with dynamic memory allocation (2)

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.