In C++, are custom header files optional? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
This was asked on a past exam. Given that the header file is custom I am assuming not because they are just variations of the main(){ header correct?

I'm not sure if I understand the question, but I would say yes, they are optional. You could write all of your (custom) functions, classes, etc. in one file if you wanted.

I'm new here so I can't comment, but the question is a bit confusing to me. But here is what I think, I hope it helps:
Header files contain functions, variables, classes etc. in C and C++. Header files that come pre-built with your compiler must be included before you can use any function or anything thing inside that file.
Now referring to a custom header files, you may choose to create a file containing specific information to use in your programs, often to make your code look more organize or to create reusable libraries. Those are OPTIONAL simply because you can manage to create all your functions, variables and classes in the same file containing your main(){}. It might look messy, impossible to read but possible to achieve.
BTW I'm not sure about what you mean by header files being variations of the main(), but agreeing with Trevor Hickey, they shouldn't have a main() function since they are not compilable, they don't execute the functions they just hold the information.

Related

C++ include header / forward declaration order [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 3 years ago.
Improve this question
A similar issue has already been discussed (C/C++ include header file order), but this thread makes no mention of forward declarations. If I summarize what I've read online so far:
Everyone agrees that the corresponding header for the .cpp file should go first. This ensures that the header files has everything it needs.
Beyond that, there seems to be no consensus. The Google guidelines (https://google.github.io/styleguide/cppguide.html) suggest including headers from system -> other libs -> project. Many people on SO suggest the exact opposite. It seems to be a matter of personal preferences.
Regarding forward declarations, is there any reason to add them before/after the include headers? I don't see why it would matter (Is a class declaration allowed after a class definition?), but maybe I am missing something.
I think this is one of those questions that has no one right answer.
The way I think of it (and its by no means the absolute answer) is from an encapsulation point of view and I tend to do the opposite of the google guidelines for this reason:
If you include your project headers first you are less likley to hit issues where your header relies on some system include that went before it because you will just get a compile error... so therefore I find you see issues faster by putting the system includes last.
Same thing for the forward declarations - if you need them and they are not provided by a header - then add them after the includes because the headers should not need them (otherwise it would be in the header) and so you only declare it as/when you need it...
That is more-or-less my reasoning - but as I say, if your headers are designed correctly then you could do it either way...

In MFC/C++ taking too much time to build [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am having one big project in which I have kept all Headers in one common header file and where all I needed i included that header file.. by doing like this project is working fine but if I do changes in any header file its taking too much to build so I want to know is there any resolution to reduce the build time?
This isn't really specific to MFC, it is a general C++ thing. Basically don't put everything in 1 common header. Make use of forward declarations wherever possible. Use include guard macros in headers unless doing some special preprocessor magic.
Use a precompiled header and only put stuff in there that very rarely changes. Don't let this header get too big though as that can decrease build times.
Reduce the amount of code in headers. In some cases the pimpl idiom can make headers more terse and less prone to change due to 'internal' implementation changes at the cost of run-time efficiency.
http://www.cplusplus.com/forum/articles/10627/

Documenting code header and source [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 9 years ago.
Improve this question
It's a simple question. I'm documenting my code following the documentation of the Doxygen, but I have a question: Where I need to document? On the header, on the source or both of them? I know that this is not a rule, but I want to know your opinion about this and what you do on your code and why you do that. For example: I'm documenting the header, and it's nice, the appearance of the code increased, but when I look the source (.cpp) file, I got scared, because there's no documentation and the code is not beautiful, I mean, not the logic, but in beauty (indentation). And everybody knows that even though the code not beautiful (which is difficult in C++) with a documentation it get coolest and easier to read.
Thank you all. (And remember, before you start writing a moral lesson, know that I know that it's not a rule, I'm just wondering what you do)
Personally I prefer documenting the h file with that sort of thing. People who need to use your APIs might need your header, but not your cpp. People will never need your cpp without the header. Keep the documentation in one place where everyone who needs it gets it.

disallow access outside of file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Now I know someone is going to say static or anon namespace without reading so let me just say no that is not what I'm looking for. What I am looking for is something that will allow me to kind of "quarantine" off a file in my code base so it can't access anything outside of that file so that if someone changes it it can't inadvertently screw things up elsewhere. Is this possible?
What I am looking for is something that will allow me to kind of "quarantine" off a file in my code base so it can't access anything outside of that file so that if someone changes it it can't inadvertently screw things up elsewhere. Is this possible?
For the most part, no, not as a part of the C++ language.
In order to accomplish your goal, consider one/some of the following:
Moving the code from your file into another library to reduce the likelihood of collateral damage
Providing "guarantees" by testing with dynamic tools like valgrind, Purify, ASan ("Address sanitizer"), Electric Fence
Making comments regarding the intended design of the code for this file ("isolated", "encapsulated", etc)
Build-time restrictions: dump the preprocessed output from the source file, flagging cases where new #includes (ones outside of a whitelist, e.g.) show up.
Have the file not include any headers from the rest of your project. Of course this doesn't protect against malicious coding, but then, neither does anything else in C++.

structure reading C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following problem:
I have a configuration file that consists a description of fields , which I read it and then parse it. I want to move it into the code to compile it inside.
How would you do that as bug structure ??? or else ?
Thanks
I wouldn't move it into the code, I'd leave the configuration file as a configuration file.
If you really must do this, you can just embed the file as a string resource into the application and use that - that way you'd change only a minimal amount of existing code. The way you do this depends upon your platform.
If thats not feasible (for whatever reason) I'd set up a single configuration class / namespace to contain all the values.
It's not very clear what are you exactly asking.
If you are looking for on-the-fly code execution (like eval() function in some languages), then there is no such thing in C++. It's not an interpreted language which can be read and executed line-by-line, it needs to be compiled every time code changes. While it technically is possible to write self-changing code, it's probably not worth the effort.