Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to "uninclude" a file in C++?

Tags:

c++

class

I have a class that is only used in one specific part of another class. It is not mentioned in any other parts of the main class. And I include the corresponding header at the top of a file.

Is there any way to "uninclude" the header after I've used it?

What I mean, is that after you "uninclude" the header with the class, references to the class would not be defined, and any calls to the class would throw an error when compiling.

Pseudo-code:

// code section where "cubeData" should be visible
#include "CubeData.h"

void mainWidget::createGeoObjects() {
  cube = new cubeData();
  // ...
}

// #uninclude "CubeData.h" - ???
//
// code section where "cubeData" should be invisible
void mainWidget::otherFunction() {
  cube = new cubeData(); // should result in a compile error
  // ...
}

like image 915
new QOpenGLWidget Avatar asked Dec 23 '20 21:12

new QOpenGLWidget


2 Answers

The effect of unincluding a header half way through a code file, into which you have included the header at the file start, is supposed to be that the code in the second half acts as if the header has not been included.

This can be achieved by splitting the code file into two parts.
First part includes the header in question (and probably some other headers).
Second part does NOT include the header (but the others again, so that their content IS known).

You might have to put anything from the first half which needs to be visible in the second half into a new header and also include that into both parts.

So in short, create two code files and include what you need into them. Especially do not include what you do not want to be visible.

like image 172
Yunnosch Avatar answered Nov 15 '22 07:11

Yunnosch


No.

Part of this is that includes are part of the preprocessor. For the actual compiler, code from an included file and code from the including file look the same.

like image 43
mrks Avatar answered Nov 15 '22 09:11

mrks