Restrict a class object to be allocated only on the stack or only on the stack.

Source: Internet
Author: User

1. Set the class constructor attribute to private, and provide the static member function create to create a new object in the function. The implemented class can only be allocated on the stack.

Class base {

Protected: static base * single; base () {} public :~ Base () {} static base * Create () {single = new base (); Return single;} void destroy () {Delete this ;}}; base * B = base:: Create ();


2. Reload the new operator of the class to empty the function of the new operator after the reload. In this way, the outer program cannot allocate objects on the stack, but can only allocate objects on the stack.

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.