Search code examples
c++header-files

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


Solution

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