C ++ static member variable nesting class allocation reclaim memory

Source: Internet
Author: User

Memory for allocating Nested classes:

# Include <stdio. h>

# Include <assert. h>

Class CA

{

Public:

Inline static CA * GetInstance (void)

{

Assert (m_instance! = NULL );

Return m_instance;

}

Void Print (void)

{

Puts ("run the Print function of the main class ");

}

Class Garbage // nested class used to allocate memory

{

Public:

Garbage (int I, int j)

{

Printf ("Garbage memory allocation start: % d % dn", I, j );

M_instance = new CA (123123,234 234234 );

}

~ Garbage ()

{

Delete m_instance;

Puts ("Garbage completion ");

}

};

Static Garbage m_garbage;

Protected:

Friend class Garbage;

CA (int I, int j) {printf ("Main class constructor run: % d % dn", I, j );}

~ CA () {printf ("Main class destructor running, ended oh n ");}

Static CA * m_instance;

};

CA * CA: m_instance = NULL;

CA: Garbage CA: m_garbage (2, 3 );

Int main ()

{

CA * cmb = CA: GetInstance ();

Cmb-> Print ();

Return 0;

}

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.