简体   繁体   中英

Is really required to separate c++ constructions in a .h and a .cpp files?

Well, I'm getting in C++ universe and this doubt came over.

It's too boring to have 2 files for each meaning-unit I choose to include in my program. I know I can have multiple classes in the same (pair of) archive(s), however I would like to clarify if really there's no way to write just one file, instead of a .h and a .cpp ones.

I found some other answers (like this , that and that other ) there are actually pretty explicative, but a quite older too. So hopping the language have got some improvement I came to ask:

Is there some compilation option , any another alternative extension , or whatever, that allows me to write just one file?

I appreciate it!

Okay, you need to understand what is going on. This isn't Java.

The.h file is the definition of your class. It's just an include file that can be used other places so those other places understand your class. Now, you CAN actually do the constructor inline, like this:

public:
     Foo() { ... your code here ... }

This is perfectly legal. But the problem with this is simple. Everywhere you hit this constructor, the compiler has to insert that code inline. This leads to lots of the same code everywhere you create a new Foo.

If you put the code in your.cpp file, and then compile it, you get ao file. That.o file includes a single copy of your constructor, and that's what gets called everywhere you create a Foo.

So separating the definition from the code results in smaller programs. That's less important nowadays than it used to be.

This is the nature of C++, and you should accept it.

The.h is an include file, used in other places. The.cpp is the implementation. It's not really onerous, once you grow accustomed.

You have to understand that C++ is a compiled language. When you compile a library, for example, the library contains machine-specific code. If you want to write a program that uses that library, your program has to be able to see function and class definitions to properly link that library. On the other hand, it is absolutely possible to write your entire program in header files -- indeed, the term header-only library exists to describe libraries that have no pre-compiled machine code. That means the responsibility of compiling it falls to you. You'll likely have longer compile times, and because of this, very large libraries are almost exclusively pre-compiled into binaries that are platform-specific (in the absence of a set of binaries for you machine, you must compile from source and link against the result). In theory, one could rewrite the C++ spec in such a way that only one file was necessary, but then those files would need to be present within any project that incorporates that library. For very large libraries, this can be a pain -- why include the full source of some engine when you could include just the definitions necessary to link into the binaries? This provides the added advantage of obfuscating the algorithms and implementation details from the client program. C++ is not an interpreted programming language -- it's important to think about it from the compiler's perspective.

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