I have been wondering for a while, why non-C++ OOP languages (not sure if there are other some that uses headers) doesn't have header files?
我一直在想,为什么非C ++ OOP语言(不确定是否有其他一些使用标题)没有头文件?
Well, what concerns me the most is that in C++ I can code a class header, and sends it to my mate to compile other code against it, while I am happy coding the actual implementation of the class on my own pace.
好吧,最让我担心的是,在C ++中我可以编写一个类头,并将它发送给我的伙伴以编译其他代码,而我很乐意按照自己的步调编写类的实际实现。
How can this be achieved in languages that doesn't have headers? Or maybe, is there something I am missing in my understanding?
如何在没有标题的语言中实现这一目标?或许,在我的理解中是否有一些我缺少的东西?
How can I make a class contract with other colleagues, so that both can code in their pace? Should I just do some mock class without any implementation for methods, just with mock returns, to satisfy this need or how?
我如何与其他同事签订课程合同,以便两者都可以按照自己的步调进行编码?我应该只做一些没有任何方法实现的模拟类,只需要模拟返回,以满足这种需求或如何?
2 个解决方案
#1
5
Header files are a legacy thing from the time compilers did not have the power necessary to compile all your source files without the hints from the developer.
从编译器没有编译所有源文件所需的强大功能开始,没有开发人员的提示,头文件是遗留物。
Modern languages (and in fact C++ can do so, too) use interfaces or base classes to implement design-by-contract.
现代语言(事实上C ++也可以这样做)使用接口或基类来实现按合同设计。
#2
1
C and C++ are very old languages, created at a time when the compiler needed advance declaration of identifiers in order to do a compilation in one pass and minimal memory usage (which was a major performance consideration at the time). The main purpose of header files is to provide those declarations, so the compiler can faciliate the necessary stack frame setups, memory allocations, type checks etc. etc.
C和C ++是非常古老的语言,在编译器需要提前声明标识符以便在一次传递中进行编译和最小内存使用(当时这是一个主要的性能考虑因素)时创建。头文件的主要目的是提供这些声明,因此编译器可以提供必要的堆栈帧设置,内存分配,类型检查等。
More recent languages revolve around "cleaner" language structures, and get the information that C/C++ needed headers for either by convention, inspection, deduction, or a combination of those, at the cost of some flexibility and performance. The former is not always needed, the latter largely irrelevant given the performance of the systems we use today...
最近的语言围绕“更清晰”的语言结构,并以一些灵活性和性能为代价,通过约定,检查,演绎或其组合获得C / C ++所需的信息。前者并不总是需要,后者在很大程度上与我们今天使用的系统的性能无关......
As for the "class contract"... in C/C++, your library might define thousands of functions, and regardless which ones you declared in your "exported" header, they are all accessible and can conceivably be called directly. The same is true with more recent languages; basically, documentation took the place of declaration (i.e., making access of those functions "easy"), but they are all there to be called anyway.
至于“类合同”......在C / C ++中,您的库可能会定义数千个函数,无论您在“导出”标题中声明哪些函数,它们都是可访问的,并且可以直接调用。最近的语言也是如此;基本上,文档取代了声明(即,使这些函数的访问“简单”),但无论如何它们都被称为。
So technically you didn't need C++ headers for "design by contract" either. You could just have told the customer to "declare void foo( int )
and call that". Modern languages just removed the intermediary and point you to the docs right away. ;-)
所以从技术上讲,你也不需要“按合同设计”的C ++标题。你可以告诉客户“声明void foo(int)并调用它”。现代语言只是删除了中介并立即指向您的文档。 ;-)
#1
5
Header files are a legacy thing from the time compilers did not have the power necessary to compile all your source files without the hints from the developer.
从编译器没有编译所有源文件所需的强大功能开始,没有开发人员的提示,头文件是遗留物。
Modern languages (and in fact C++ can do so, too) use interfaces or base classes to implement design-by-contract.
现代语言(事实上C ++也可以这样做)使用接口或基类来实现按合同设计。
#2
1
C and C++ are very old languages, created at a time when the compiler needed advance declaration of identifiers in order to do a compilation in one pass and minimal memory usage (which was a major performance consideration at the time). The main purpose of header files is to provide those declarations, so the compiler can faciliate the necessary stack frame setups, memory allocations, type checks etc. etc.
C和C ++是非常古老的语言,在编译器需要提前声明标识符以便在一次传递中进行编译和最小内存使用(当时这是一个主要的性能考虑因素)时创建。头文件的主要目的是提供这些声明,因此编译器可以提供必要的堆栈帧设置,内存分配,类型检查等。
More recent languages revolve around "cleaner" language structures, and get the information that C/C++ needed headers for either by convention, inspection, deduction, or a combination of those, at the cost of some flexibility and performance. The former is not always needed, the latter largely irrelevant given the performance of the systems we use today...
最近的语言围绕“更清晰”的语言结构,并以一些灵活性和性能为代价,通过约定,检查,演绎或其组合获得C / C ++所需的信息。前者并不总是需要,后者在很大程度上与我们今天使用的系统的性能无关......
As for the "class contract"... in C/C++, your library might define thousands of functions, and regardless which ones you declared in your "exported" header, they are all accessible and can conceivably be called directly. The same is true with more recent languages; basically, documentation took the place of declaration (i.e., making access of those functions "easy"), but they are all there to be called anyway.
至于“类合同”......在C / C ++中,您的库可能会定义数千个函数,无论您在“导出”标题中声明哪些函数,它们都是可访问的,并且可以直接调用。最近的语言也是如此;基本上,文档取代了声明(即,使这些函数的访问“简单”),但无论如何它们都被称为。
So technically you didn't need C++ headers for "design by contract" either. You could just have told the customer to "declare void foo( int )
and call that". Modern languages just removed the intermediary and point you to the docs right away. ;-)
所以从技术上讲,你也不需要“按合同设计”的C ++标题。你可以告诉客户“声明void foo(int)并调用它”。现代语言只是删除了中介并立即指向您的文档。 ;-)