简体   繁体   English

没有标题的c ++类

[英]c++ class without header

Ok, so I don't have a problem, but a question: When using c++, you can transfer class to another file and include it without creating header, like this: 好的,所以我没有问题,但是有一个问题:使用c ++时,您可以将类转移到另一个文件并包含它而无需创建标头,如下所示:

foo.cpp : foo.cpp:

#include <iostream>
using namespace std;

class foo
{
public:
   string str;
   foo(string inStr)
   {
       str = inStr;
   }
   void print()
   {
      cout<<str<<endl;
   }
};

main.cpp : main.cpp:

#include "foo.cpp"
using namespace std;

int main()
{
   foo Foo("That's a string");
   Foo.print();
   return 0;
}

So the question is: is this method any worse than using header files? 所以问题是:这种方法是否比使用头文件还要糟糕? It's much easier and much more clean, but is it any slower, any more bug-inducing etc? 它更容易使用,也更干净,但是会更慢,是否会引起更多错误? I've searched for this topic for a long time now but I haven't seen a single topic on the internet considering this even an option... 我已经搜索该主题很长时间了,但是考虑到这个选项,我还没有在互联网上看到一个主题...

There's no semantic difference between naming your files .cpp or .hpp (or .c / .h ). 命名文件.cpp.hpp (或.c / .h )没有语义上的区别。

People will be surprised by the #include "foo.cpp" , the compiler doesn't care 人们会对#include "foo.cpp"感到惊讶,编译器不在乎

You've still created a "header file", but you've given it the ".cpp" extension. 您仍然创建了“头文件”,但给了它“ .cpp”扩展名。 File extensions are for the programmer, the compiler doesn't care. 文件扩展名是给程序员的,编译器不在乎。

From the compiler's point of view, there is no difference between your example and 从编译器的角度来看,您的示例与

foo.h : foo.h:

#include <iostream>
using namespace std;

class foo
{
  //...
};

main.cpp : main.cpp:

#include "foo.h"
using namespace std;

int main()
{
   // ...
}

A "header file" is just a file that you include at the beginning ie the head of another file (technically, headers don't need to be at the beginning and sometimes are not but typically they are, hence the name). “头文件”只是您在头文件中包含的文件,即另一个文件的头文件(从技术上讲,头文件不必在文件头,有时也不必,但通常是头文件,因此是文件名)。

You've simply created a header file named foo.cpp . 您仅创建了一个名为foo.cpp的头文件。

Naming header files with extension that is conventionally used for source files is not a good idea. 通常,将带有扩展名的头文件命名为源文件不是一个好主意。 Some IDE's and other tools may erroneously assume that your header is a source file, and therefore attempt to compile as if it were such, wasting resources if nothing else. 某些IDE和其他工具可能会错误地认为您的标头是源文件,因此尝试像编译头文件那样进行编译,否则就浪费了资源。

Not to mention the confusion it may cause in your colleagues. 更不用说它可能引起您的同事的困惑。 Source files may have definitions that the C++ standard allows to be defined exactly once (see one definition rule, odr) because source files are not included in other files. 源文件可能具有C ++标准只允许定义一次的定义(请参阅一个定义规则odr),因为其他文件中未包含源文件。 If you name your header as if it were a source file, someone might assume that they can have odr definitions there when they can't. 如果您将头文件命名为源文件,则有人可能会认为无法在其中具有odr定义。

So the question is: is this method any worse than using header files? 所以问题是:这种方法是否比使用头文件还要糟糕?

You might consider reviewing the central idea of what the "C++ translation unit" is. 您可以考虑复习“ C ++转换单元”是什么的中心思想。

In your example, what the preprocessor does is as if it inserts a copy of foo.cpp into an internal copy of main.cpp. 在您的示例中,预处理器的工作就像将foo.cpp的副本插入到main.cpp的内部副本中一样。 The preprocessor does this, not the compiler. 预处理器执行此操作,而不执行编译器。

So ... the compiler never sees your code when they were separate files. 所以...当编译器是单独的文件时,编译器从不会看到您的代码。 It is this single, concatenated, 'translation unit' that is submitted to the compiler. 正是这个单一的,串联的“翻译单元”被提交给编译器。 There is no magic in .hh nor .cc, except that they fulfill your peer's (or boss's) expectations. .hh和.cc中没有魔术,只是它们满足了您的同事(或老板)的期望。

Now think about your question ... the translation unit is neither of your source files, nor any of your system include files, but it is one stream of text, one thing, put together by the preprocessor. 现在考虑您的问题……翻译单元既不是您的源文件,也不是您的系统的任何包含文件的文件,但它是预处理器将一堆文本(一件事)组合在一起。 So how would it be better or worse? 那么它会好还是坏呢?


It's much easier and much more clean, 它更容易,更干净,

It can be. 有可能。 I often take this 'different' approach in my 'private' coding efforts. 我在“私人”编码工作中经常采用这种“不同”方法。

When I did a quick eval of using gmpxx.h (mpz_class) in factorial, I did indeed take just these kinds of shortcuts, and did not need a .hpp file to properly create my compilation unit. 当我快速评估一下阶乘使用gmpxx.h(mpz_class)时,确实确实采用了这些快捷方式,并且不需要.hpp文件即可正确创建我的编译单元。 FYI - The factorial of 12345, is more than 45,000 bytes. 仅供参考-12345的阶乘是超过45,000字节。 It is pointless to read the chars, too. 读字符也毫无意义。

A 'more formal' effort (job, cooperation, etc), I always use header's, and separate compilation, and the building a library of functions useful to the app as part of how things should be done. 作为“更正式”的工作(工作,合作等),我总是使用头文件,并进行单独的编译,并建立对应用程序有用的函数库,作为应做事情的一部分。 Especially if I might share this code or contribute to a companies archives. 特别是如果我可能共享此代码或为公司档案做贡献。 There are too many good reasons for me to describe why I recommend you learn these issues. 我有太多充分的理由来描述为什么我建议您学习这些问题。


but is it any slower, any more bug-inducing etc? 但这会更慢,会引起更多错误吗?

I think not. 我想不是。 I think not. 我想不是。 There is one compilation unit, and concatenating the parts has to be right, but I think is no more difficult. 有一个编译单元,连接各个部分必须正确,但是我认为这不再困难。


I've searched for this topic for a long time now but I haven't seen a single topic on the internet considering this even an option... 我已经搜索该主题很长时间了,但是考虑到这个选项,我还没有在互联网上看到一个主题...

I'm not sure I've ever seen it discussed either. 我不确定我是否看过它。 I have acquired the information. 我已经获得了信息。 The separate compilations and library development are generally perceived to save development time. 通常认为分开的编译和库开发可以节省开发时间。 (Time is money, right?) (时间就是金钱,对吗?)

Also, a library, and header files, are how you package your success for others to use, how you can improve your value to a team. 此外,库和头文件是如何将成功打包给他人使用的方法,以及如何提高团队价值的方法。

If you ever build some larger project, the two main differences will become clear to you: 如果您曾经构建过一个更大的项目,那么两个主要区别将对您显而易见:

  1. If you deliver your code as a library to others, you have to give them all your code - all your IP - instead of only the headers of the exposed classes plus a compiled library. 如果将代码作为库交付给其他人,则必须将所有代码(全部IP)提供给他们,而不是仅提供公开类的头以及已编译的库。

  2. If you change one letter in any file, you will need to recompile everything . 如果您在任何文件中更改一个字母,则将需要重新编译所有内容 Once compile times for a larger project hits minutes, you will lose a lot of productivity. 一旦大型项目的编译时间缩短了数分钟,您将失去很多生产力。

Otherwise, of course it works, and the result is the same. 否则,它当然可以工作,并且结果是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM