Two ways to create objects in C + +

Source: Internet
Author: User

In C + +, in order for a class to be created only through new (that is, if the object is created directly, the compiler will give an error), it should ()

The correct answer: B your answer: D(Error)
To make a constructor private
To set a destructor to private
Set constructors and destructors to private
There's no way to do it

I didn't think much of it at the time and thought it was impossible. It's easy to think about it afterwards, but it's actually a memory management problem.

Can only do heap on create object

In C + +, the creation of classes is divided into two types. One is static creation, that is, the object is created directly, and the other is the dynamic creation of the object, which is created through new , such as t *t = new T. To answer the question correctly, you have to know the difference between the two ways to create it.

1 Static creation

The compiler allocates memory for the object in the stack, obtains the appropriate size by moving the top pointer of the stack, and then calls the object's constructor to generate the object.

2 Dynamic Creation

Creates an object in the heap with new. This process is divided into two steps: first find the appropriate size space in the heap and allocate it, and then call the object's constructor to generate the object.

Because both need to invoke the constructor of the object, it does not work by privatizing the constructor . So is there any other way? This is another feature of static creation that needs to be understood.

When the compiler allocates stack space for a class object, it checks the class's destructor for access, in fact not just the destructor, as long as the non-static function, the compiler will check. If a class's destructor is private, the compiler does not allocate memory on the stack space for the class object.

So we just need to privatize the destructor to organize the creation of objects directly. Since the creation and release of the stack need to be done by the system, if the constructor or destructor cannot be called, the error will be made.

Of course, in order for us to properly release dynamically created objects, we must provide a public function, the only function of which is to delete the object itself .

The test code is as follows:

#include <iostream>using namespace Std;class test{private:~test () {cout << "test Destroy" << Endl;} Public:void destroy () {delete this;}}; int main () {//test p;//compiler Error Test::~test () not accessible test *p = new Test;p->destroy ();}

objects can only be created on the stack now that we are able to create objects on the heap only, we can create objects on the stack only. Actually understand this idea, it is not difficult to think that we only need so that the new operator is not available,To do this, we can The new operator is overloaded and set to private accessCan. is not a very clever way ~

Overloading new while preferably overloading the delete

#include <iostream>using namespace Std;class test{private:void* operator new (size_t t) {}void operator delete (void * ptr) {}public:~test () {cout << "test Destroy" << Endl;}}; int main () {//test *a = new test;//compiler Error function Test::operator New is not accessible test A;
Above is the dynamic creation on the heap, the following is the static creation on the stack
}

Two ways to create objects in C + +

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.