Answer the question
In order to leave comments, you need to log in
How to split a class into files?
Hello.
I read about that as it is correct to carry classes on files. Interfaces (.h) separately, implementation (.cpp) separately. But I have a question. I am writing a class (for example, for working with vectors or matrices) - most of the functions are either inline or contain a maximum of 2-3 lines. Spread such a class into several files... um...
How bad form would it be to implement all such a class in one file (in .h for example)? And what is the right thing to do in this situation?
Answer the question
In order to leave comments, you need to log in
The principle is simple. You can only put in .h what does not produce code. As soon as the second CPP appears in the project and uses this header, the code will be produced twice, and the linker (cl/ld/ilink) will swear that the variable or function is duplicated. What exactly doesn't produce code...
• Macro definitions. They basically do not produce code.
• Declaration of any type. It only speaks of a device of this very type; the code is produced by those who use this type.
• Templates. The code does not produce the template itself, but the fact of unpatterning. Of course, the template can be unmasked in two compilation units, but we have learned to deal with this automatically.
• inline - the code does not produce inline itself, but the fact of inclusion. inline can be both an explicit keyword and an implicit one in the class body.
• Prototypes and extern - they say: the code is there, but somewhere not here.
• Constexpr C++11. They substitute the value.
• Some const depending on the compiler. For example, on Borland, const double produces code, but const int does not.
They produce code and are prohibited in headers.
• Variable without extern, even const.
• A function that is not inline.
• A fully specialized template with no template parameters left ( template<>
).
They don't produce code, but it's better to throw it into CPP.
• Some private inline and templates if they are not used from the header.
How bad form would it be to implement all such a class in one file (in .h for example)?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question