简体   繁体   English

如何在C ++中仅包含头文件中的一个符号

[英]How to include only one symbol from a header file in C++

I have a big header file, but in a program, I only need a few specific variable defined in that header. 我有一个很大的头文件,但是在程序中,我只需要在该头中定义一些特定的变量。

Is there any way in c++ that I tell it to include only that specific variable ? 在C ++中,有什么办法可以让我告诉它仅包括该特定变量?

And, yes, the variable has to be in that header file only. 而且,是的,变量必须仅在该头文件中。

What is the disadvantage if I include the entire header file (as I am doing it now) ? 如果包括整个头文件(现在正在做)有什么缺点?

You don't need to include the header for a single variable, just declare it locally: 您不需要为单个变量包含标头,只需在本地声明它即可:

extern Type variable;

There's no fundamental disadvantage to including a header file you need. 包含所需的头文件没有根本的缺点。 It will increase compilation time, especially if you change the header and compile multiple times, but for clarity it should be included. 这将增加编译时间,尤其是如果您更改标头并多次编译时,但为清楚起见,应将其包括在内。 Think of the case where you rename the variable, or change its type - you don't want to rename it in hundreds of places that use it (although some IDE's make this fairly easy), you want to have a central access point. 考虑一下重命名变量或更改其类型的情况-您不想在使用它的数百个地方重命名它(尽管有些IDE相当容易),但您想要一个中央访问点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM