#pragma once与#ifndef的区别(ZZ)

本文介绍了在C/C++中防止头文件被重复包含的两种常见方法:#ifndef/#define和#pragma once。前者通过宏定义检查来避免重复包含,后者依赖编译器支持。详细对比了两者的优缺点及适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 为了避免同一个文件被include多次

1   #ifndef方式
2   #pragma once方式

在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一些细微的区别。
    方式一:

    #ifndef __SOMEFILE_H__
    #define __SOMEFILE_H__
    ... ... // 一些声明语句
    #endif

    方式二:

    #pragma once
    ... ... // 一些声明语句


    #ifndef的方式依赖于宏名字不能冲突,这不光可以保证同一个文件不会被包含多次,也能保证内容完全相同的两个文件不会被不小心同时包含。当然,缺点就是如果不同头文件的宏名不小心“撞车”,可能就会导致头文件明明存在,编译器却硬说找不到声明的状况

    #pragma once则由编译器提供保证:同一个文件不会被包含多次。注意这里所说的“同一个文件”是指物理上的一个文件,而不是指内容相同的两个文件。带来的好处是,你不必再费劲想个宏名了,当然也就不会出现宏名碰撞引发的奇怪问题。对应的缺点就是如果某个头文件有多份拷贝,本方法不能保证他们不被重复包含。当然,相比宏名碰撞引发的“找不到声明”的问题,重复包含更容易被发现并修正。

   方式一由语言支持所以移植性好,方式二 可以避免名字冲突

提示报错NXOpen::Point3d没有成员DistanceTo,。参考下下面的帮助文档,以下是参考文档:#ifndef NXOpen_UGMATH_HXX_INCLUDED #define NXOpen_UGMATH_HXX_INCLUDED //-------------------------------------------------------------------------- // Header for C++ interface to JA API //-------------------------------------------------------------------------- // // Source File: // ugmath.ja // // Generated by: // apiwrap // // WARNING: // This file is automatically generated - do not edit by hand // #ifdef _MSC_VER #pragma once #endif #include <NXOpen/NXDeprecation.hxx> #include <vector> #include <NXOpen/NXString.hxx> #include <NXOpen/Callback.hxx> #ifdef _MSC_VER #pragma warning(push) #pragma warning(disable:4996) #endif #ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif namespace NXOpen { /** Represents three-dimensional cartesian coordinates for a point <br> Created in NX3.0.0. <br> */ struct Point3d { public: /** x */ double X; public: /** y */ double Y; public: /** z */ double Z; public: Point3d() : X(), Y(), Z() { } /** Constructor for the Point3d struct. */ public: Point3d(double xInitial /** x */ , double yInitial /** y */ , double zInitial /** z */ ) : X(xInitial), Y(yInitial), Z(zInitial) { } }; /** Represents a three-dimensional vector <br> Created in NX3.0.0. <br> */ struct Vector3d { public: /** x */ double X; public: /** y */ double Y; public: /** z */ double Z; public: Vector3d() : X(), Y(), Z() { } /** Constructor for the Vector3d struct. */ public: Vector3d(double xInitial /** x */ , double yInitial /** y */ , double zInitial /** z */ ) : X(xInitial), Y(yInitial), Z(zInitial) { } }; /** Represents a 3 x 3 matrix <br> Created in NX3.0.0. <br> */ struct Matrix3x3 { public: /** xx */ double Xx; public: /** xy */ double Xy; public: /** xz */ double Xz; public: /** yx */ double Yx; public: /** yy */ double Yy; public: /** yz */ double Yz; public: /** zx */ double Zx; public: /** zy */ double Zy; public: /** zz */ double Zz; public: Matrix3x3() : Xx(), Xy(), Xz(), Yx(), Yy(), Yz(), Zx(), Zy(), Zz() { } /** Constructor for the Matrix3x3 struct. */ public: Matrix3x3(double xxInitial /** xx */ , double xyInitial /** xy */ , double xzInitial /** xz */ , double yxInitial /** yx */ , double yyInitial /** yy */ , double yzInitial /** yz */ , double zxInitial /** zx */ , double zyInitial /** zy */ , double zzInitial /** zz */ ) : Xx(xxInitial), Xy(xyInitial), Xz(xzInitial), Yx(yxInitial), Yy(yyInitial), Yz(yzInitial), Zx(zxInitial), Zy(zyInitial), Zz(zzInitial) { } }; /** Represents two-dimensional cartesian coordinates for a point <br> Created in NX6.0.0. <br> */ struct Point2d { public: /** x */ double X; public: /** y */ double Y; public: Point2d() : X(), Y() { } /** Constructor for the Point2d struct. */ public: Point2d(double xInitial /** x */ , double yInitial /** y */ ) : X(xInitial), Y(yInitial) { } }; /** Represents four-dimensional homogeneous coordinates for a point. To convert to three-dimensional cartesian coordinates, divide x, y and z by w. <br> Created in NX8.5.0. <br> */ struct Point4d { public: /** x */ double X; public: /** y */ double Y; public: /** z */ double Z; public: /** weight */double W; public: Point4d() : X(), Y(), Z(), W() { } /** Constructor for the Point4d struct. */ public: Point4d(double xInitial /** x */ , double yInitial /** y */ , double zInitial /** z */ , double wInitial /** weight */) : X(xInitial), Y(yInitial), Z(zInitial), W(wInitial) { } }; } #ifdef _MSC_VER #pragma warning(pop) #endif #ifdef __GNUC__ #ifndef NX_NO_GCC_DEPRECATION_WARNINGS #pragma GCC diagnostic warning "-Wdeprecated-declarations" #endif #endif #endif
最新发布
08-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值