Is it possible to separate classes in multiple header files?

I have a class that has several functions and many operators. Almost all functions and operators use templates, so I implemented them in the header file. This made it difficult to find anything in the code, so I decided to move all statements into a separate header file.

now i have something like:

fstring.h

class fstring{
    ...
    #include "fstring_operators.h"
}

      

and fstring_operators.h

...
template<int RSIZE>
bool operator==(const fstring<RSIZE>& rhs) const {
    return equals(rhs._chars, RSIZE);
}
...

      

Can you do something like this? I also missed the header protection for fstring_operators.h because it shouldn't be included anywhere other than fstring.h

+3


source to share


2 answers


While I've seen this in production code before, I disagree with this style for two reasons:

1) You expect to be fully defined in the title. You don't need to look into other titles to find what you're looking for.



2) You can include a different title elsewhere. Even without the guards, it’s not a guarantee it won’t work.

+7


source


I think you should define methods as free functions. You can then # include at the bottom of a regular header file, which is a practice many C ++ libraries use (some of which would force statement file names to end in .i or .inl or something).



If you do this, everything will be fine. The way you wrote it in the OP is a bit of a stranger, and even if it works, it might dump some maintainers in your code and maybe even some development tools.

+4


source







All Articles