Defines a class that can only construct objects on the stack

Source: Internet
Author: User

Only objects defined on the heap can be private by declaring the constructor, and then provide two static methods, a method used to get the objects on the heap, and one to free the objects on the heap. The second way to define a class that cannot be inherited in C + + is to declare a class that cannot be inherited in this way, but the object of this class can only be in the heap.


So how about a certain object that can only be constructed on the stack?

This involves the new operator in C + +, which contains two steps:

    1. Call a global operator new function to allocate a certain amount of memory space
    2. Calling the constructor of an object

If a class is not allowed to allocate memory on the heap, but allows it to allocate memory on the stack, compare these two ways to discover that only the first step is forbidden, which is to prohibit an object from invoking the operator new function. This may involve the operator new library function, for the class, this library function can be overloaded, only need to overload it into private. This is a description of operator new in C + + reference. You can see the description of operator new in this link. The following function prototype is copied from the above document. This can even be accomplished without writing.

#include <iostream> #include <new>using namespace Std;class a{public:a () {cout<< "A con" <<ENDL; ~a () {cout<< "A des" <<ENDL;} Private:static void* operator new (std::size_t size) throw (std::bad_alloc); static void* operator new (std::size_t size, C Onst std::nothrow_t& nothrow_value) throw (), static void* operator new (std::size_t size, void* ptr) throw ();}; int main () {a A; A * ap=new a (); return 0;}

The output hint error can be found:


If you remove the second line of code from the main function, the function will work:

That means it can construct objects on the stack.


At the same time, looking at the new process above, we found that new can actually use the memory on the stack to construct the object. is to use the placement new version, which constructs the object on the stack if it is provided to the address on the new stack. Note the call destructor needs to be displayed when you use the placement new version.

#include <iostream>using namespace Std;class a{public:a (int t=1) {d=t;cout<<d<< "A con" <<ENDL;} int d;~a () {cout<<d<< "A des" <<endl;}}; int main () {const int len=sizeof (A); char Buf[len]; A * a=new (BUF) A (2);//Call Placement new This version a->~a ();//Note the call destructor needs to be shown here because the memory in the BUF is in the stack, so the memory is released after the function jumps back to 0;

Execution output:




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Defines a class that can only construct 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.