|
I tend to (loosely) follow the guideline: if the file (header or source) doesn't need an header then it should not include it.
Speeding up compilation is more an issue with C++ than with C (passing from C++ to C is the real performance boost on compilation times, in my experience).
|
|
|
|
|
That's what I thought... I've been doing it mostly wrong for 7 years Thanks!
GCS d--(d+) s-/++ a C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- r+++ y+++* Weapons extension: ma- k++ F+2 X
|
|
|
|
|
|
I never put includes into a header file if I can avoid it using forward declarations. Any source code file including that header then can decide for itself whether it needs (and must include) the full declaration.
There's no benefit to put includes into headers other than saving a little typing in your source files that may need some more include statements than usual. (and you could actually reduce or avoid that if you just put commonly used headers in your .PCH file instead)
But there's a high risk in doing so: macros breaking other source files (e. g. the min and max macros #defined when including windows.h breaking STL code); the global name space cluttered with thousands of symbols that may conflict with local names; and compilers silently resolving a function call to something different than you expect because of such conflicts.
There are several benefits to avoiding includes in headers, such as reducing response times of any tool or functionality that requires parsing your code (including your language-sensitve text editor and compiler), and avoiding unnecessary clutter in your global namespace. It's also informative to see what the declarations inside of a header (or source) file really depend on.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
I am going to disagree with almost all above in C the standard industry practice is the only time a header is declared in the header file is if an actual interface call requires a type in the header. There simply is no other valid reason you should ever do it because of caching blah blah blah others mentioned.
The basic premise is why would you want a dependency on the interface at compile time if the interface doesn't need to know the dependency. The header is there as clean interface segmentation layer not somewhere for you to play around with speed ups and your own personal playground.
Internally in the C file if your C code requires the header declare it in the .c file again because it is required!!!!!!
Basic rule you never include anything in any file that is not absolutely required .... that is the end of the story.
I am anal and actually record why I am including the unit with a comment... so this is a typical example
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
So on your example I would never declare windows.h in my unit header unless I had a function in the C file that needed the interface to pass a windows type again I would comment why the header is included and as an example
#include <windows.h>
void MyUnitFunction (HWND window);
If I had the situation no public interface needed internal windows types (they were just using normal c types etc) the #include <windows.h> would only ever be in the .C file and not the header file as I don't need to tell the interface.
So for me no includes don't always go in the header files or in the C files they go where they are absolutely required !!!!!!!
You should find that alone will stop lots of stupid circular problems you would get otherwise. If you have two units that go circular and you really can't organize them better then as already mentioned above in one unit forward declare the other units interface functions. Generally you choose the one with the least functions to forward.
In vino veritas
modified 30-Jan-19 9:35am.
|
|
|
|
|
How i check word in row and column in 2D word search Game in c++?
|
|
|
|
|
It depends. If you know in advance what is the word, then the program would simply scan rows and columns of the matrix of letters, searching for the word.
On the other hand, if you don't know in adavance the word, then you have to use a dictionary (and possibly invent an algorithm smarter than the brute force).
|
|
|
|
|
i wanna ask, what is the best of windows programming for C++ now? is it still using win API or is there any better one than Win API?
|
|
|
|
|
It's always up to you and the kind of the task you are going to solve.
For a usual GUI application I always choose the MFC. It saves me a lot of time comparing wit the plain Win32 API.
However there is a lot of tasks that are not covered by MFC. So I just write some plain Win32 code or sometimes just simple C-runtime one.
|
|
|
|
|
For quick utilities, MFC is still a good way to go. For a large application, check out Qt.
|
|
|
|
|
WIN32 API any framework including MFC just adds another layer of abstraction and the issue that the frameworks itself often becomes incompatible with newer windows versions. There is already a growing list of bugs with MFC and Windows 10.
Conceptually if you want a C++ objectification of the Win32 API then an old framework called Win32++ is still around it does nothing other than take the standard Win32 API into objects it doesn't mess around with the message system like MFC and is completely opensource. It also doesn't have the problems with .NET integration that MFC has.
C++ Win32 Framework for Beginners[^]
Last revision 8.6 was done nov 2018
Win32++ download | SourceForge.net[^][^]
Win32++ - Browse /Win32++/Version 8.6.0 at SourceForge.net[^]
Probably worth adding that if you are interested in the cutting edge windows area of WSL (Windows subsystem running linux) you can throw a X server on a Windows 10 64 bit box and start working out how to encapsulate linux GUI. You can guess where MS is going with this (MS hasn't announced any frameworks but some public test ones have appeared GitHub - kpocza/LoWe: Linux on Windows extender[^])
One company has already hit the WSL space hard
https://www.whitewaterfoundry.com/[^]
In vino veritas
modified 29-Jan-19 1:00am.
|
|
|
|
|
Hi folks,
I am relatively new to c++. Currently I am trying my best to create a MS Word document out of my c++ program. I found a few tutorials on Word automation which gave me good initial results (like opening a document, writing plain text, saving documents etc).
However, my goal is to insert more complex things like a table of content, headlines, footers and so on and I couldn't find any usable information about that.
Therefor I have 2 questions:
1. Is office automation really the best way to achieve such a goal? (the official Microsoft support page seems a little outdated on that topic).
2. Most articles I found are about MS Excel. Unfortunately, I need to work with MS Word. Can somebody point me towards a comprehensive c++ -> MS Word article?
Thanks for your time & help 
|
|
|
|
|
|
Thank you! Those were the tutorials I used so far. They recommend to use the macro codes in MS Word to find the desired functions. However, I have trouble converting the macro code in c++.
For example: To create a headline, the makro shows this code:
Sub Makro()
Selection.Style = ActiveDocument.Styles("Überschrift 1")
End Sub
("Überschrift 1" means "Headline 1").
Any ideas?
|
|
|
|
|
I'm not sure I understand what you're aiming to accomplish.
If you want to write a C++ program that constructs a Word program, the first response might besomewhat helpful.
If you want to produce a Word dcument that contains your program code with some added formatting a table of contents, and maybe some useful internal links, you might want to take a look at Doxygen: Main Page[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
When breaked at debugging, I can press F10, line by line to view the current execution of the source code. Suppose my program has 1000 lines of code,
I don't want to press F10 1000 times to watch all the 1000 line in the order of execution.
I wish output whole 1000 lines of code in execution order.
surce code like below:
id fun(int* piVal)
{
*piVal = 0;
}
main()
{
int iTemp = 0;
fun();
iTemp = 1;
}
I need a text file after running the program. The contents of the file are like below:
int iTemp = 0;
*piVal = 0;
iTemp = 1;
text file has 3 line, It exact save the three lines of execution.
|
|
|
|
|
Member 13081369 wrote: I need a text file after running the program. The contents of the file are like below:
int iTemp = 0;
*piVal = 0;
iTemp = 1;
text file has 3 line, It exact save the three lines of execution.
And what are you going to do with such an output?
|
|
|
|
|
If the code like below:
if(iVal < 10)
{
FunA();
}
else
{
FunB();
}
When first run, iVal=1. So the first.txt Should be:
if(iVal < 10)
FunA();
And second run, iVal=20. The second.txt Should be:
if(iVal < 10)
FunB();
I could compared two file(first.txt and second.txt) to find code execution difference.
I need it, because my codes very very larged. when iVal differeced every times, the differece appeard at after hundreds lines between first.txt and second.txt.
|
|
|
|
|
It's hard to understand your question. I think there are many better ways of solving the problem which might include restructuring the program, especially to make that section into smaller modules or to create your own debugging interface and pass parameters through it so you have a functional means of tracking program states. If you could tighten up input validation and narrow the scope of acceptable input it may help detect bugs.
The way you want to do it printing code is not even remotely a standard functionality or common practice so it is a very oversized foot you are trying to shoehorn into a small shoe and the result will likely be as comfortable.
If you want to pursue it anyway it's hard to imagine without knowing your code. You could set up a custom table of watch variables and or expressions and use the stringizing operator in a macro, similar to an assert() style.
#define record_str (a) (fprintf(debug, "%s : %s", #a, a))
#define record_int(a) (fprintf(debug, "%s : %s", #a, itoa(a)))
Combine it with __LINE__ and or FILE or TIME
C/C++ line number - Stack Overflow[^]
Hope this helps
|
|
|
|
|
|
You can try using "run to cursor" to see if that helps.
|
|
|
|
|
Thanks, I known how to "using run to cursor",but the problem is I need to pressed F10 button hundreds, for execution whole program.
I need to view "execution whole program line by line" exacts
|
|
|
|
|
Well good luck, but I don't think you are going to find it.
|
|
|
|
|
Neither 'Run to cursor' nor 'step over' (F10) are suitable functions for your problems. Either set a breakpoint within the code of the right if/else branch, or set a data breakpoint that stops execution the moment iVal changes its value: How to: Set a Data Breakpoint (Native Only) | Microsoft Docs[^]
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
A line by line code execution logger doesn't make any sense. The sheer amount of data that would produce for any kind of meaningful program will swiftly exceed any storage capacity that you can throw at it. Not to mention that it would increase computation time by a factor of 100-1000 due to the required IO operations.
Much better you learn to use the debugger: simply place a break point at the branch(es) you're interested in and let the debugger run until it hits one of your break points.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|