-3

I have this header file for C++ that's supposed to make file input easier to code, but I was it to be memory efficient.
I have a class in my header file that looks like this:

class FileInputOutput {
    #include <fstream>

But I'm not sure if it would include that anyways like the #include statements at the top, or if it would just include it when the class was called.

Can anyone help me out?

  • This answers the question for user supplies includes: http://stackoverflow.com/questions/7347033/including-headers-inside-class-declaration-definition – NathanOliver Mar 01 '17 at 19:44
  • For standard headers IMHO you should only ever include them in the global space at the beginning of the header. – NathanOliver Mar 01 '17 at 19:45
  • 1
    @NathanOliver This example uses a standard header. I can hardly imagine a scenario where including a standard header in a class definition would work. – François Andrieux Mar 01 '17 at 19:46
  • 1
    @FrançoisAndrieux I believe you are right I'm just not certain if it would never work. – NathanOliver Mar 01 '17 at 19:47
  • 1
    @NathanOliver: Even if would always work, it certainly has no bearing on the proposed goal: *"I wa[nt] it to be memory efficient."* – IInspectable Mar 01 '17 at 19:50
  • 1
    @NathanOliver _"I'm just not certain if it would never work."_ Weird wrapper and template trickery? Not as long anything is consolidated in the `std` namespace. That cannot appear inside a class declaration. – πάντα ῥεῖ Mar 01 '17 at 20:38

3 Answers3

5

Is it possible to #include in a C++ class?

As for your question title: It is possible, yes.

You should note though that the c(++) preprocessor just expands the text from the #include'd file, and the c++ validation is checked in another phase of compilation.

Code like

class FileInputOutput {
#include <fstream>
};

will inject all the text from fstream inside your class.

That's most probably not what you want.

Just to use the class declarations from fstream do

#include <fstream>

class FileInputOutput {
    std::fstream fs;
};
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
2

The Preprocessor in C++ (and in C) behaves by effectively copying the text of the referenced file into the file that used the #include statement.

So when you write code like this:

Something.h

int my_rand() {
    return 4; //chosen by dice roll, guaranteed to be fair
}

Main.cpp

#include "Something.h"

int main() {
    return my_rand();
}

The final file before the proper compilation starts looks like this.

int my_rand() {
    return 4; //chosen by dice roll, guaranteed to be fair
}

int main() {
    return my_rand();
}

In your case, writing something like

class my_class {
    #include<fstream>
/*...*/
};

Will result in some very strange behavior, and will almost certainly not compile, because the entire contents of <fstream> will be copied into the middle of your class, and it's extremely unlikely that you've written your class in a way that will accomodate that, or that <fstream> is written to handle being injected into a different class.

There are some cases where you can #include in the middle of your program like that, however. Observe:

My Resource.txt

R"D(
Blah Blah Blah Blah Blah
)D"

Main.cpp

int main() {
    std::string blah_string = "" //The "" is unnecessary, but I use it to make Intellisense shut up
#include "My Resource.txt"
    ;
    std::cout << blah_string << std::endl;
    return 0;
}

Which will correctly print out

Blah Blah Blah Blah Blah

To the console, because it's the same as writing

int main() {
    std::string blah_string = 
R"D(
Blah Blah Blah Blah Blah
)D"
    ;
    std::cout << blah_string << std::endl;
    return 0;
}
Xirema
  • 19,889
  • 4
  • 32
  • 68
1

#include just results in copy/paste of the included file contents by the preprocessor during compilation. So yes, sure, you can do that, but it doesn't buy you anything efficiency wise. It's just as if you had written the contents of the included file directly inline in the including file.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70