-2

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?

Xandor
  • 440
  • 1
  • 9
  • 22
  • 3
    You should learn about the [one definition rule](https://en.cppreference.com/w/cpp/language/definition). If you define your functions in headers you risk violating it as the complexity of your project grows. – François Andrieux Jan 11 '19 at 18:58
  • Possible duplicate of https://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files – François Andrieux Jan 11 '19 at 18:59
  • 2
    @FrançoisAndrieux your comment omits some crucial parts, including inline functions and templates. – SergeyA Jan 11 '19 at 19:01
  • 1
    @SergeyA That's what the link is for. My comment is a short summary. No use repeating the whole thing. – François Andrieux Jan 11 '19 at 19:02
  • @FrançoisAndrieux I think you misunderstood my question. I am not asking about defining or declaring in a header. I am asking if printing output in a header is considering "doing something" and thus bad practice. Your comment does not answer my question and your "duplicate" is not at all what I am asking. – Xandor Jan 11 '19 at 19:11
  • @Xandor There's nothing special about outputting in this context. Note that if I thought my comment answered your question, I would have made it an answer. And note that the second comment is a *possible* duplicate, depending on the interpretation of the question, inviting others who share that interpretation to vote to close or upvote the comment to see if the community can reach a quorum. – François Andrieux Jan 11 '19 at 19:14
  • 1
    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. – user4581301 Jan 11 '19 at 19:15
  • @FrançoisAndrieux Fair enough, and I apologize if my response came across as hostile. I was simply trying to clarify what I was asking so there were less issues of interpretation of the question. – Xandor Jan 11 '19 at 19:29
  • @user4581301 I'm not sure I understand what you are getting at? In my header file I can do something like `std::cout << "fooBar";` and it works fine. The include in my .cpp file for the header is after `#include ` and thus works fine. Are you saying this for the use case of if my header is included into a project that doesn't have `#include ` it will not compile? I didn't think it was that crazy for a header file to require other includes in the *.cpp file. With that being said I am asking about best practices so this might be relevant if you could clarify for me. – Xandor Jan 11 '19 at 19:37
  • 1
    @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. – François Andrieux Jan 11 '19 at 19:40
  • @FrançoisAndrieux So then where are classes to be defined? I thought it was considered best practice to define classes in a header file. This class happens to contain functions pertaining to the class object. From what you are saying it sounds like this is/should be the root of my concern. – Xandor Jan 11 '19 at 19:51
  • Classes are really neat. A method fully defined in the class effectively becomes an inline function (there are a couple differences, but none that matter here) and sidesteps the One Definition Rule. A variable defined in the class doesn't exist until the class is instantiated, also getting around ODR. – user4581301 Jan 11 '19 at 19:55
  • @Xandor It really seems like the linked possible duplicate could answer a lot of your questions. This is not a cope out to avoid answering your question. That question's answers will likely clear up a lot of your misunderstanings. – François Andrieux Jan 11 '19 at 19:58
  • I think I may understand now. I found an example online where a class object is split into two files. `class.h` and `class.cpp` where the .h file declares them and the .cpp defines them. I guess this really is my whole issue. To me, as a self taught programmer I see no reason to even have the .h file then but what I'm reading says you "should". Why? – Xandor Jan 11 '19 at 19:58
  • @Xandor That's exactly what the linked possible duplicate asks. What's the reason for having separate .h and .cpp files. – François Andrieux Jan 11 '19 at 19:58
  • I need to correct myself. C++11 Standard says, [A function defined within a class definition is an inline function.](https://timsong-cpp.github.io/cppwp/n3337/dcl.fct.spec#3) I guess there are no differences, at least not any more. – user4581301 Jan 11 '19 at 19:59
  • @FrançoisAndrieux Apologies, your comment had not populated on my screen while I was writing my last one. Yes, that link does clear that up, I just at first did not understand the heart of my issue. I apologize for the back and forth about this. – Xandor Jan 11 '19 at 20:01
  • @Xandor It's not a problem, it happens a lot. It can take time for all parties to converge on a common understanding. – François Andrieux Jan 11 '19 at 20:02

3 Answers3

4

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.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • 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. – Xandor Jan 11 '19 at 19:22
  • They teach C++ to accountants now? World is getting more and more hardcore. – user4581301 Jan 11 '19 at 19:33
  • Not at all. https://cppinstitute.org/cpa-c-certified-associate-programmer-certification – Xandor Jan 11 '19 at 19:40
  • @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 Jan 11 '19 at 21:56
  • @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. – Xandor Jan 12 '19 at 20:10
1

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 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. – Xandor Jan 11 '19 at 19:23
1

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);
eerorika
  • 232,697
  • 12
  • 197
  • 326