Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to split classes in multiple header files?

Tags:

c++

I've got a class that consists of a few functions and a lot of operators. Almost all functions and operators use templates which is why I've implemented them in the header file. This made it pretty hard to find something in the code so I decided to move all operators into a seperate headerfile.

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);
}
...

Is it ok to do something like this? I've also omitted header guards for fstring_operators.h because it must not be included anywhere except in fstring.h

like image 744
Markus Avatar asked Dec 17 '22 01:12

Markus


1 Answers

Although I've seen this in production code before, I don't really agree with this style for 2 reasons:

1) You expect a class to be fully defined in the header. You shouldn't need to look inside other headers to find what you're looking for.

2) You can include the other header elsewhere. Even without include guards, it's not a guarantee it won't work.

like image 197
Luchian Grigore Avatar answered Jan 06 '23 03:01

Luchian Grigore