views:

132

answers:

3

I have a number of places where I need to re-use some template code. Many classes need these items

In a .h could I do something like:

#include <xxx.txt>

and place all of this code in the .txt and just have it included?

Something like a PHP include!

Does gcc allow this?

+6  A: 

You can include any file you care to name.

EDIT: You could also have just tried it. Worst case? You have an "undo" on your hands.

Chris
@Chris - I did using a .inc file extension and it did not work for me. GCC complained about missing items.
JT
Can you post an exact error message? It seems to me like probably your .inc file just has some invalid syntax.Remember, any file you #include can contain only valid C++ code.
Chris
+3  A: 

don't forget to wrap your various items in #DEFINE's and #IFDEF's to make sure you aren't redefining junk all over the place for your compiler to bitch about...

Zak
A: 

As Chris says, you can include any file you care to name.

In practice it's best to avoid arbitrary file extensions - I'd suggest 'SomeFile.inl' over 'SomeFile.txt', as your IDE of choice is more likely to apply correct syntax highlighting to the file when you open it.

HulkHolden