Closed. This question is opinion-based. It is not currently accepting answers.
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.
std::cout in an application-specific file, be it a header file or a .cpp file, is perfectly fine.
C++ cout - C++ Standard Library, were introduced into C++ to resolve identifier name conflicts. It is particularly bad to use 'using namespace std' at file scope in header files. Using it in source files (*.cpp) at file scope after all includes is not quite as bad, as its effect is limited to a single translation unit.
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.
What is std and what does it means? - C++ Forum, allow us to put declarations in one location and then import them wherever we need them. Why “using namespace std” is considered bad practice The statement using namespace std is generally considered bad practice. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.
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);
Why "using namespace std" is considered bad practice , The statement using namespace std is generally considered bad practice. Although the statement saves us from typing std:: whenever we wish to access a class or type defined in the Let us say we wish to use the cout from the std namespace. Consider for example the code for getting the current time in the program. What to put in a header file. Because a header file might potentially be included by multiple files, it cannot contain definitions that might produce multiple definitions of the same name. The following are not allowed, or are considered very bad practice: built-in type definitions at namespace or global scope; non-inline function definitions
2.11, You make the declarations in a header file, then use the #include directive the .cpp file, and we include <iostream> to pull in the declaration for std::cout . The following are not allowed, or are considered very bad practice:. Here is an working example of what you want to do. Read the comments to know what each line in the code does. I've tested it on my pc with gcc 4.6.1; it works fine. Header files (C++), Hi, I was wondering why code like std::cout is used instead of putting “using namespace std;” to avoid writing std:: all the Why is "using namespace std" considered bad practice? The only important rule is just to never put it in a header file. Including header files from other directories. Another common question involves how to include header files from other directories. One (bad) way to do this is to include a relative path to the header file you want to include as part of the #include line. For example: [code] #include “headers/myHeader.h” #include “../moreHeaders
Why use "std::cout" instead of “using namespace std;”?, Should I type “std::” instead of having “using namespace std”? global namespace in a header file, than that is a bad practice: a header file is intended add using std::cout INSIDE the function which uses it, or alternatively inside the cpp file A simple practice in C or C++ programs is that we keep all the constants, macros, system wide global variables, and function prototypes in the header files and include that header file wherever it is required. Include Syntax. Both the user and the system header files are included using the preprocessing directive #include. It has the following
Comments You should learn about the one definition rule. If you define your functions in headers you risk violating it as the complexity of your project grows. @FrançoisAndrieux your comment omits some crucial parts, including inline functions and templates. @SergeyA That's what the link is for. My comment is a short summary. No use repeating the whole thing. You can't print with
cout without defining a function or defining and initializing a variable. Neither of which you want to do in a header without first considering ODR.
@Xandor user4581301's point is that
std::cout << "fooBar"; will only work if your write it in the context of a function body. Meaning any issue with printing in a header is at least as problematic as writing a function in a header (because it means that if you are printing, you are also necessarily writing a function) making the link between your question (printing in headers) and the problem with writing functions in headers in general.
While both current answers more or less answer my question, this one more directly talks about my concern and thus is the accepted answer to me. I for the most part agree about making a conscious decision myself and I usually do. My main goal is to pass the CPA exam. I am a rather experienced programmer in other languages and am now shifting to C++. Recently in an interview one of the key points they made was that I didn't follow best practices (I'm paraphrasing) thus my question is more directly about best practices rather than mechanics. My understanding of the CPA is that it follows "best practices" more than real world applications. They teach C++ to accountants now? World is getting more and more hardcore. Not at all. cppinstitute.org/… @Xandor if your goal is to pass a certification exam, then you should probably study the materials associated with the exam to learn their best practices. Best practices are not universally agreed upon. @eerorika Absolutely. I completely agree. But given the subject of my question it does pertain to that exam. I can learn hacks and tricks all day long to get around certain issues (like put all my code in one file, or just go ahead and define classes as a whole in a header file) but this does me no good. Obviously I am going to want to do something with that cert and thus need to learn good coding habits if I plan to work on a team.