Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

local #includes

Tags:

c++

include

Is there some way to #include standard things locally (for one function, one class, etc... at a time) instead of globally. Taking a very simple example one might want to use std::string but it is only needed in one class and you don't want the overhead of it existing everywhere.

like image 498
alan2here Avatar asked Dec 16 '22 18:12

alan2here


2 Answers

Instead of making #include local, you should probably just move the class that requires it into a separate file.

like image 150
casablanca Avatar answered Dec 27 '22 15:12

casablanca


Since #include is simply just a kind of text replacement before compilation (by the preprocessor), it is just a matter of where you put the include statement.

Likely, you are refering to "locally" as "in just one .cpp file" and to "globally" as "in all .cpp files".

If this is true, you can make an #include as "locally" by only including in that .cpp files where you want it. If you want to include a file with one #include statement in several files, place the include statement into a .h file and include that .h file inside all required files.

A good place to make a "global" #include is the .h header file that serves as the precompiled header.

like image 33
Uwe Keim Avatar answered Dec 27 '22 14:12

Uwe Keim