0
I cannot understand why we separate classes definition and content.
the lesson in this app introduce the method of application of classes. in some examples, they first declare the class in a header then write the content in the cpp
3 odpowiedzi
+ 8
It speeds up compilation times, as you can forward declare, and you don't have to recompile it each time you make a change. It's also much cleaner and readable.
+ 4
DISCLAIMER: I'm intentionally giving the simplistic version of this explanation. By all means, anyone feel free to clarify further or correct me where I might be wrong.
----------------------------
Header files originated during a time when disk space, memory, and processing power were very limited. They were created to aid the compiler by explicitly declaring all methods prior to their implementation code being compiled.
The compiler may be in the process of compiling a reference to a method call before the implementation code for that method has been compiled. However, since the method interface has already been declared via the header files, the compiler can trust that the implementation code will exist, even though it hasn't yet compiled it.
Alternatively, modern "strongly typed" languages, such as C# and Java, were created when limited computer hardware wasn't an issue. These languages were created such that method interfaces and namespaces could be quickly resolved without having to explicitly create header files.
This is similar to how "loosely typed" scripting languages (Python, JavaScript, Ruby, PHP) work, except interfaces are resolved during runtime execution of the code rather than during compile time.
[UPDATE]
@aklex's explanation would be the TL;DR; version of my answer. ;)
+ 1
Let's say a customer asks you to build a module for a game. You build it in C++, compile in the right environment and provides the header and the compiled module (unlinked, usually with the extension .o). After that, your customer can use your code with the interface, linking the module, without knowing how it was implemented. If he needs to change anything in the code, he has to ask you to do so, because it is a black box to him. If you provided the implementation, his team could learn how to maintain and improve the code and they wouldn't need your company anymore. That is just one of the reasons why we separate it.