Why use a C++ header file? [duplicate] - c++

This question already has answers here:
Why have header files and .cpp files? [closed]
(9 answers)
Closed 8 years ago.
I understand the header file contains all the prototypes of every function contained in the implementation file; but what is the real purpose? Is it because when another program calls on the class, it only receives the information in the header file, and the implementation is kept hidden? What is the real purposes of this?

The purpose is that when someone gives you a library, they don't always give you the code. And frankly you don't want to know about it. You simply need to know the function prototypes and data structures it provides, which you obtain from the Header File. And then you link to the library.
Here's an example that might help make some sense of it for you:
I publish a library libfoo (doesn't matter if its a .so, .a or .dll) and told you it had a bunch of good functions and data type in it that solve a problem and you want to use it. If I didn't give you the header file, how would your code possibly compile when it would need to know how big data structures are / what function signatures to look for? How would you know how to code it?
The library won't be referenced until linking, and compilation is already complete at that point in time.

Related

What exactly should I declare in a header file in C++? [duplicate]

This question already has answers here:
What should go into an .h file?
(12 answers)
Closed 2 years ago.
I have searched almost every website I could, but nobody has been able to answer my question. Basically what I ask, is what exactly should I place in a header file? I know of function declarations, but are variable declarations in a .h file also a thing? I need to know what the "tradition" or the "usual method" is of usage of header files.
EDIT : If this helps, I just recently started C++ and my objective with learning this language is an opportunity to get into the industry, mainly gamedev, and I need to know what the proper way of doing stuff is. Hence the question.
Header files conventionally contain declarations of entities that you wish to share between translation units.
That usually means class definitions, type aliases, and function declarations.
A translation unit, very roughly speaking, is "a .cpp file".
So, if you use a function in a.cpp, and you also use it in b.cpp, a declaration for it needs to be available in both translation units, and the best way to do that is to write it once in a header file, then include the header file in both .cpp files.
That's it.
While it's possible to declare variables in a header file, it's rare to do so. For starters, you'd want to put extern on it to make it not a definition (you don't want multiple definitions!). At that point, you have to ask yourself why you're not using a class.

Is std::cout in a header file bad practice? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
This might be a duplicate question but all of my search results talk about not putting using into a header file, which is not my question. I have read up on this and understand why you should not place using inside of a header file.
I am new to C++ and am trying to make sure I am learning/using best practices. I know header files are meant for defining classes, structs and the like and should never "do" anything. Does this include std::cout? Is that considered doing something? What I mean is, is it okay to print output from a header file, or should i return data to my *.cpp file and do the output from there? Or does this not really matter?
There is nothing wrong in principle with doing things in header file. Indeed, header only libraries are quite popular in C++ nowadays. In some cases (such as templates) doing things in header file is the only way to go.
The art of splitting definitions between header file and .cpp file is often a judgment call. Generally, when you define functions in header file, you might hope for better performance (since inlining would be more easily achieved), but you might end up with larger codebase (depending on linker behavior), and you are likely to increase your compilation time.
Instead of asking for best practices, I wholeheartedly suggest you understand what are the mechanics at play there, and make a conscious choice yourself.
IMHO ...
Use of std::cout in any file in a library is a symptom of poor design. If you need to output something, provide the interface for the client code to pass a ostream or an ostream-like object that supports inserting data to it.
Use of std::cout in an application-specific file, be it a header file or a .cpp file, is perfectly fine.
Is std::cout in a header file bad practice?
Not necessarily. For example, you might have a function which outputs character sequence into a stream. It would be useful to let the client of the function to choose which stream to use, so the function accepts the stream as an argument. It might make a lot of sense for the function to have the default behaviour of streaming to the standard output stream. Therefore you might have a function declaration in a header such as:
void stream_fancy_stuffs(std::ostream& output_stream = std::cout);

Hiding definition of C++ template class member functions [duplicate]

This question already has answers here:
How to make closed source for the library written with template in C++
(3 answers)
Closed 6 years ago.
C++ Compiler needs to have access to the implementation of the methods in order to instantiate a template class. Hence we need to either include the definitions of a template in the header file that declares that template or define them in header files. But I want to hide the implementation (definitions) of my functions from my users due to many reasons and want to ensure that they have only access to the function declarations. Is it possible to do so while using templates in C++?
This is usually done (for eg) in boost by creating a directory hierarchy impl and detail.
impl will usually have the implementatios for the exposed API member function while inside the detail as you might have guessed, has the gritty details which you do not want to expose. This system is fairly well understood by the C++ developers.
As an example:
my_service.hpp
impl/my_service.hpp
impl/my_service.ipp ( OR )
detail/my_service.ipp
So, the idea is to split the header files and provide the users to only include your top level header file which will internally include the other files.
But nobody is stopping the users to include the impl header file as well directly. If you want to avoid that too, then you can do something on the lines of Prevent header from being included in some files, in compilation time?

Pracitcal design difference between header and cpp files [duplicate]

This question already has answers here:
Why have header files and .cpp files? [closed]
(9 answers)
Closed 6 years ago.
Comming from a mostly Java background I recently started learning a bit of C++.
I'm trying to create a class that describes a LinkedList. Since I want this class to be something like an external library, so I can use it in other applications, I need it to be declared as a header file.
I understand that from a design point of view C++ needs both a header and a cpp file, where the header will mostly contain declarations and the implementation will be left for the cpp file.
What I don't understand though is what is the actual purpose of the above design technique. The point of being able to quickly go through the header file and read the comments about the functionality of each method seems moot to me, since C++ (or at least my Codeblocks editor) supports the
/** comments like Java so it is much easier to create an object of the class and simply hit the . button to open the methods description menu, and read any description I like in a more orderly and structured manner than going through a header file.
Also I noticed that the program runs fine if put everything inside the header file and completly ignore the cpp file.
So my question boils down to whether I should just implement everything in the header file, or whether this is considered unacceptable C++ coding practice and if so, why.
Headers contain the bare minimum required to be able to use the functionality, since they get to some intermediate preprocessed file. The smaller that file, the easier to compile that unit.
They have to contain structure definitions and function declaration in this sense. This is unlike Java which doesn't require prior declaration in order to use and is a historical artifact which cannot be fixed now. The #include lines really paste the text in the header within the actual C/CPP file (sort of -- the preprocessing, conditional compilation etc have to happen in the headers too)

Template Class Link-Time Error [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file??
Hi,
I have a c++ project which consists of several header and source files. In order to modify the class with a template, I found that all the method definitions must be included in the header file itself, else resulting in a link-time error. What is the reason for this and how can I get over this ??
Thank You!!
This is intentional. Templates go in header files, end of story. There is no way around it. In order for the compiler to instantiate the template it must be able to see it, which means it must be in a header file. Templates are not like C# generics, where the model allows them to be hidden an essentially instantiated by the linker - templates are much more complicated than that, so they have to live in header files.
In your travels you will find mention of the export keyword, which was supposed to solve this. To cut a long story short, it didn't, no one implemented it (apart from one very brave compiler vendor) and it will soon not be part of C++ any more.