Placement new embarrassment in the MFC project

Source: Internet
Author: User

In the past few days, anglescript has a new understanding of C ++. For example, placement new

This is actually a global new operator that cannot be overwritten. It is used to reconstruct existing objects. Re-build, rather than re-allocate, so it can only work on the allocated memory space. This is exactly the meaning of "placement ".

For exampleInt* P =New int[5];

New(P)Int[5];

This is legal.

WhileInt* P;

New(P)Int[5]; // an error occurs during the execution period. Because p is not allocated beforehand.

In fact, placement new is to re-call the object's constructor.

The younger brother was a beginner and just learned about this operator in recent days. He first saw some anglescript simple operators on the Internet to construct objects. I didn't understand what it meant at the beginning. I just copied it to my project. However, it was not possible to pass the compilation, and I didn't care at the time. I thought it was an action to create an object from heap, So I replaced it with P = new OBJ. But it will soon be reflected. This will not work, because P points to a reallocated space in this statement. Script has allocated space for P, but it is re-constructed with new parameters.

But why can't I find the answer after Google's compilation. Therefore, a common console is created.Program. The compilation of the same statement does not seem to be a problem with the compiler. Compared with the compiler options of two projects, there is no essential difference. Strange.

The <New> header file contains a Conditional compilation.

# Ifndef _ Placement_new_inline

Didn't I define it?

After definition, the afxtempl. h compilation error is prompted.

Locate the error and the answer will be discovered.

# Pragma push_macro("New ")
# UNDEF new
::New((Void*) (& Pnode-> data) type;
# Pragma pop_macro("New ")

Damn it, they don't want to tell me, too harsh.

Return to <New> and find the two definitions at the bottom of the file.

# Ifdef _ msc_ver
# Pragma pop_macro("New ")

nnd, killed by them. But it is actually usable. Share it with you. You may encounter similar problems one day.

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.