disallow access outside of file [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 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++.

Related

Which library - std::chrono::system_clock [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 3 years ago.
Improve this question
Which .dll file contains "std::chrono::system_clock" method?
I can't find any useful information in the documentation.
EDIT:
I want to create my own version of the function, so I wanna rebuild the file and replace it in the import table of my own application
All you need is to #include <chrono>. No further deployment needed.
Well, without knowing your actual goal ("knowledge over rules", you said) it is impossible to tell you the right way to achieve it. I can only tell you that this is not it.
I just want to do that if it is possible
For some value of possible, maybe.
It's possible that the code for this type is defined right there in the header, rather than built into the runtime. So, you would only "need" to modify the header. But then what about the rest of the runtime that expects the type to look a certain way? You've just violated the one-definition rule at the first hurdle.
It should go without saying:
DO NOT DO THIS.
Your program has undefined behaviour when you modify the standard library, even if you can somehow manage to get enough controls of your implementation's internals to make it work without crashing and/or catching fire.
To do this "properly" you would have to fork and rebuild the entire standard library implementation so that its contents are consistent. Good luck with that.
Instead, if you want to use a type that looks like std::chrono::system_clock (it is not a "method", or member function — it is a class) but acts differently, then define such a type yourself in your own namespace.
People have been doing this, when mocking implementation functions, for many years. Your unit testing framework should come with some documentation that teaches you how to mock functionality successfully and reliably.

Is using windows.h a good thing? [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 6 years ago.
Improve this question
I'm just curious about whether using windows.h a good thing (PS : I don't care about OSindependent code) ,it seems really good with thousands of functions . So is it good ? and where is its Dir ?
Good, bad or indifferent depends on the view.
If you are programming Windows-specific applicaitons, then you'll probably not be able to avoid using it. For generic applications that work on any platform, it's obviously bad, and if you DON'T know that you need some headerfile, don't include it. But do include things that you do need, even if it "works" without - you never know when something changes that header file that dragged in something else you didn't include in your code, and breaks the code.
"Where is it's dir", I presume means "what directory will I find it in", and I'm afraid that's not something I, or anyone else, can tell you. It depends on which compiler you are using, and how/where it was installed - and in some cases, you need to install it separately, in other cases it's included with the compiler. Since there are at least half a dozen different current compilers that work under Windows, and several that no longer are being maintained, but still "works" [to varying degrees], it would be rather pointless to even try to answer this.
Microsoft does publish an "SDK", which contains the headers, should they not be part of your installation of the compiler:
https://dev.windows.com/en-us/downloads/windows-10-sdk

In C++, are custom header files optional? [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 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.

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.

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.