#pragma once and #ifndef

Source: Internet
Author: User

#pragma once: Ensure that header files are compiled only once. This way, is unique to the Microsoft compiler, but also later, so know not many people, with the people are not many, because he does not support cross-platform. If you want to write cross-platform code, it's best to use the previous one. This is a way that the compiler provides support to prevent two compilations of the same file, where the same file refers to a physical file.

#ifndef   #define // Code #endif

This is a C + + language-related, which is a macro definition in the C + + language that avoids multiple compilations of files through macro definitions. So it works on all compilers that support the C + + language, and it's best to use this approach if you're writing programs that cross-platform

Comparison:

#ifndef的方式依赖于宏名字不能冲突, this will not only guarantee that the same file will not be included multiple times, but also ensure that two files with exactly the same content will not be included accidentally. Of course, the disadvantage is that if the macro name of the different header accidentally "crash", it may lead to the header file exists, the compiler insists that cannot find the state of the statement
#pragma once is guaranteed by the compiler: the same file will not be included more than once. Note that the "same file" here refers to a physical file, not two files of the same content. The advantage is that you don't have to bother thinking about a macro name, and of course there's no strange problem with the macro name collision. The disadvantage is that if a header file has multiple copies, this method does not guarantee that they will not be included repeatedly. Of course, duplicate inclusions are more susceptible to discovery and remediation than the "no claims" issue caused by macro name collisions.

#pragma once and #ifndef

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.