C + + Storage allocation + Dynamic memory allocation + setting limits + initializer list (1)

Source: Internet
Author: User

1. The object's space is allocated at the beginning of the parentheses, but the construct is implemented when the object is defined, and if skipped (such as Goto), an error occurs when the parentheses end the destructor, but the compilation is not.

2. Initialization

1 structY ZintIfloatFCharc;};2 3-X x1 = {1,2.2,'C'};4X x2[3] = { {1,1.1,'a'},{2,2.2,'b'} };5 6 7 structYfloatFintI Yinta);} ;8 9Y y1[] = {Y (1), Y (2), Y (3)};

3. Default constructor = = Parameterless constructor

Y y2[2] = {Y (4);}   // It is wrong

4. New/delete

NEW: Allocate space + call constructor

Delete (delete[]): First destructor, then reclaim space

int New int [ten]; Delete [] psome;     // can't lose []

Do not use the delete to free space that is not new

Do not delete the same memory space two times

When using new[], you must use delete[]

New does not take [],delete nor

A null pointer to delete is safe (nothing will happen): not sure if new is used

No delete. Memory leaks.

int*p =New int;int*a =New int[Ten]; Student*q =NewStudent (); Student*r =Newstudent[Ten];DeleteP//address and size of Pa++;Delete[] A;//run error, unable to find the address of a at newDeleteQ//Recycling StudentDeleteR//the space is retracted, and the destruction only makes aDelete[] r;//Destructors All Objects

Only at compile time

--public: Common

--private: Self-member function of the class, two objects of the same kind can access the private variables with one another.

--protected:

Friend: Another class, a function inside another class.

structX//declaration of preceding paragraphstructy{voidF (x*); };structx{Private:    inti; Public:    voidInitialize (); FriendvoidG (x*,int);//Global friendFriendvoidY::f (x*);//Struct member FriendFriendvoidZ//entire struct is a friendFriendvoidh ();};

Class vs. struct

The class default is private

struct default is public

Preferred class

struct a{

Private

int i;

Public

A:I (0) {}

The order in which the assignment is implemented is before the constructor is executed, compared to the

Try to use initialization without assigning values

C + + Storage allocation + Dynamic memory allocation + setting limits + initializer list (1)

Related Article

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.