M
M
MrSayMyName2016-10-10 18:22:06
OOP
MrSayMyName, 2016-10-10 18:22:06

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

2 answer(s)
M
Mercury13, 2016-10-10
@Mercury13

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.

A
abcd0x00, 2016-10-13
@abcd0x00

How bad form would it be to implement all such a class in one file (in .h for example)?

They were specially made separate, because they are not compiled, but only inserted directly through include. Some are designed for a single insertion (then gates are made from ifndef), and some are designed for multiple insertion (then gates from ifndef are not needed). And when they are inserted, then they are already compiled in the context of the place where they are inserted.
The purpose of .h files is to make names visible (to declare names).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question