简体   繁体   中英

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:

foo.cpp :

#include <iostream>
using namespace std;

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

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 ).

People will be surprised by the #include "foo.cpp" , the compiler doesn't care

You've still created a "header file", but you've given it the ".cpp" extension. 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 :

#include <iostream>
using namespace std;

class foo
{
  //...
};

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 .

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.

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. 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.

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.

In your example, what the preprocessor does is as if it inserts a copy of foo.cpp into an internal copy of 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.

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. FYI - The factorial of 12345, is more than 45,000 bytes. 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.

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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