gpt4 book ai didi

c++ - 是否应该重新包含项目范围预编译 header 中已经存在的内容?

转载 作者:行者123 更新时间:2023-11-30 01:31:04 25 4
gpt4 key购买 nike

我有一个预编译头文件 stdafx.h,它用于我项目中的所有源文件。因此,stdafx.h 中的所有 header 在项目的所有代码文件中均可用。

我不确定是否要重新包含预编译 header 中已有的内容。大家怎么看?

例如

stdafx.h

#pragma once

#include <memory>

我的类.h

#pragma once

#include <memory> // Re-include or not that's the question. Best practice?

struct my_class
{
};
typedef std::shared_ptr<my_class> my_class_ptr;

主要.cpp

#include "stdafx.h"
#include "my_class.h"

int main()
{
}

最佳答案

我会包含它,这样头文件就可以在具有不同 stdafx.h 的项目中重用 另一种说明方式是每个头文件都应该包含它自己需要的所有声明(最好是前向声明)

不会有任何性能影响,因为由于内部头文件保护(或者对于 VS,头文件中的#pragma:once),头文件的内容将不会被处理。

关于c++ - 是否应该重新包含项目范围预编译 header 中已经存在的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3691077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com