|
It is used to prevent a header file from being included more than one time.
|
|
|
|
|
|
- I still have a question. I think we can use the statement "#if !defined ... #define" to do the same task. I think the use of "#pragma once" is a redundance. Am I right? (For example, in MainFrm.h of a MDI app)
- Can you help?
- Regards,
BigMouth
|
|
|
|
|
In most cases that I have seen it used it is redundant.
There are two ways to go about the issue of multiple include prevention. You can benignly allow it and protect against it in the header (like the AppWiz does) or you can require the includers to insure that they haven't seen it yet which is what I usually do.
I use this kind of thing :
#ifndef _THISHEADER_H
#define _THISHEADER_H
#else
#error repeated include of this file
#endif
Then in the source files I do this :
#ifndef _THISHEADER_H
#include "ThisHeader.h"
#endif
This insures that the header is included once and only once.
FWIW, I learned this from checking out the SDK headers. I added the error check so that I would find the cases where I missed the ifndefs.
|
|
|
|
|
- Thanks pal!
- I still have a question. I think the statement "#error repeated include of this file" should be #error _THISHEADER_H has already defined". I think in the header file we can only see whether the variable "_THISHEADER_H" has been defined. We can not see whether the header file has been include twice. Am I right?
- Can you help me out?
- Regards,
BigMouth
|
|
|
|
|
The reason I did it that way is that when the error message is displayed it tells you the line number and file name where it occurred.
Of course, you can make the error message say whatever you like.
|
|
|
|
|
- Thanks pal!
- I stll have a question. What means "allow it and protect against it in the header (like the AppWiz does)" in your reply? Can you give me an example?
- Regards,
BigMouth
|
|
|
|
|
What the AppWiz does is make headers like this :
#ifndef AFX_THISHEADER_H_a_whole_bunch_of_random_numbers_here
#define AFX_THISHEADER_H_a_whole_bunch_of_random_numbers_here
// contents of header go here
#endif // AFX_THISHEADER_H_a_whole_bunch_of_random_numbers_here
This style will let you repeatedly include the file because it is protected but it will not warn you about it. I like to know about and prevent against repeated inclusion.
As I said previously, this is mostly a personal preference but M$ does it also in several of the windoze headers and, in my opinion, they should because they have lots of 100K+ headers.
Many people take a bit more relaxed approach than me (including M$) and only do the #ifndef thing inside other headers when they are being nested (includes inside headers.) That's fine but I am just a bit more obssessive about it.
|
|
|
|
|
I have an intersting problem to solve that has been causing much caffeine intake over the past few hours...
Say I have a CWnd derivied class, which we'll call CBaseWnd. Now, CBaseWnd can have multiple children, which in turn can have multiple children, though only one of the first level children can be visible at a time, with any number of second...third... level children optionaly visible as you work down all branches of the family tree.
CBaseWnd has an attribute and appropriate accessor that stores whether or not the base window (itself) or any of the child windows has the focus.
Now to my question: How can I determine from within CBaseWnd if it or any of it's children, grandchildren, great grandchildren, etc, currently has the focus? I've tried all the [to me] obvious approaches, but they all cause problems when the focus is given to one of the windows in the tree from one outside it (e.g. the desktop). Why is beyond me.
There must be a way to walk through a window's children and then repeat the process for each child, etc, surely?
Any help would be greatly appreciated as I have now run out of coffee and will have to start taking coke intravenously...
P.S. Sorry about the title, but I am more used to posting the the lounge!
________________
David Wulff
http://www.davidwulff.co.uk
"I loathe people who keep dogs. They are cowards who haven't got the guts to bite people themselves" - August Strindberg
|
|
|
|
|
Hi David,
This reminds me of when I learned to recursivly traverse tree data structures
I have not tried this, but could you not use EnumChildWindows().
When the child is accessed, check if it has the focus,
If it does, send it's HWND to it's parent (say "It's me"),
If it does not, Enumerate it's own children.
If it does not get an "It's me" message from one child, go to the next,
If it does get the "It's me" message, forward it to it's immediate parent.
Do this for all levels of children until all windows are exhausted (no focus) or the "It's me" message is recieved by the top window.
Here is basic view of what I mean. (The numbers are the order windows are checked)
1Parent
+-------------------+--------------------+
2Wnd 7Wnd 10Wnd
+-----+-----+----+ +-----+ +------+------+
3Wnd 4Wnd 5Wnd 6Wnd 8Wnd 9Wnd 11Wnd Wnd Wnd
\
Focus
The focus is held by the 11th window checked, so the rest of the windows are not checked.
HTH
---
It may be that your sole purpose in life is simply to serve as a warning to others.
|
|
|
|
|
|
The problem with that is that it doesn't work for any more than the windows children (i.e. no grandchildren, etc). This is because I can not parse the child windows looking for one with focus, but rather determine the window with focus from GetFocus().
I cannot do...
if (IsChild(GetFocus()))
{
bFocus = true;
}
...but rather would need to do...
if (GetFocus() == CBaseWnd)
{
bFocus = true;
}
else if (GetFocus()->GetParent() == CBaseWnd)
{
bFocus = true;
}
.
.
.
else
{
bFocus = false;
}
... (working back down the tree from the child).
The problem is that obviously the window with the current focus may not be a child of CBaseWnd, and so navigating up it's family tree will either do no good, or crash because I reach the top too soon.
________________
David Wulff
http://www.davidwulff.co.uk
"I loathe people who keep dogs. They are cowards who haven't got the guts to bite people themselves" - August Strindberg
|
|
|
|
|
David Wulff wrote:
if (GetFocus() == CBaseWnd)
Always compare hWnd's, not CWnd pointers.
If (GetFocus()->GetSafeHwnd() == pBaseWnd->GetSafeHwnd())
cheers,
Chris Maunder
|
|
|
|
|
Thanks for that bit of wisdom!
Luckily I can't remember ever having done this before, so you nipped it in the bud!
________________
David Wulff
http://www.davidwulff.co.uk
"I loathe people who keep dogs. They are cowards who haven't got the guts to bite people themselves" - August Strindberg
|
|
|
|
|
David Wulff wrote:
The problem with that is that it doesn't work for any more than the windows children (i.e. no grandchildren, etc).
Huh... I was going by MSDN, which says:Indicates whether the window specified by pWnd is a child window or other direct descendant of CWnd. A child window is the direct descendant of CWnd if the CWnd object is in the chain of parent windows that leads from the original pop-up window to the child window.
[edit]
Oh wait, I think I mixed up owner windows with child windows. NM
--Mike--
http://home.inreach.com/mdunn/
Help! Help! I'm being repressed!!
your with and
Sonork - 100.10414 AcidHelm
|
|
|
|
|
In my head that was approaching it from the wrong angle - of course, my head isn't always right.
I read the documentation as meaning that I needed to supply the child window, but thinking about what you wrote again, of course it should work because GetFocus() would be a child (if not another window).
I believe the phrase goes something like "D'oh"...
________________
David Wulff
http://www.davidwulff.co.uk
"I loathe people who keep dogs. They are cowards who haven't got the guts to bite people themselves" - August Strindberg
|
|
|
|
|
If you want to start from the child window, why not handle the WM_SETFOCUS message in the child. When the child get's the focus, it notifies it's parent window, which would relay the message up to the top window.
I'm still guessing
---
It may be that your sole purpose in life is simply to serve as a warning to others.
|
|
|
|
|
Ah ha, I found what the problem was. Originally, one of the things I tried was:
IsChild(GetFocus())
but it didn't appear to be working correctly. However, it would seem that it was, but because for some strange reason one of the child window types is using the CBaseWnd's parent as it's parent (necessarily done), thus it was not a child of CBaseWnd, thus IsChild() returned false.
I fixed this by using:
GetParent()->IsChild(GetFocus())
instead.
Cheers. I can put the coke cans back now as I wont be needing them tonight.
________________
David Wulff
http://www.davidwulff.co.uk
"I loathe people who keep dogs. They are cowards who haven't got the guts to bite people themselves" - August Strindberg
|
|
|
|
|
I have very simple template. When I put declaration of
this template in header file and realization in cpp file then
I have got problem on link stage. Linker said that it can not
find constructor and destructor of my template.
When I placed both declaration and implementation in header
file then evrything started work.
What is the reasin of such situation?
Thanks in advice for help.
|
|
|
|
|
Maxym wrote:
What is the reasin of such situation?
That's a limitation of VC. All template code must be visible to every translation unit (CPP file), so the code has to go in a header file.
--Mike--
http://home.inreach.com/mdunn/
Help! Help! I'm being repressed!!
your with and
Sonork - 100.10414 AcidHelm
|
|
|
|
|
Thanks for help!
|
|
|
|
|
A limitation of VC? I don't think so. That's how all compilers work with templates. Since the compiler doesn't generate any code until you explicitly create a type from the template, the code for the template must be readily available to the compiler. If the code is inside some CPP, the compiler can't access it and thus cannot generate the appropriate code for the new type.
That's my understanding of why template code has to be inside header files. It's always been this way for UNIX compilers as well.
Regards,
Alvaro
|
|
|
|
|
I have a Menu (IDD_MENU1) and 2 Dialogs (IDD_DIALOG and IDD_DIALOG1). The menu has an item that will go to IDD_DIALOG1 from IDD_DIALOG when clicked. How can I make this possible?
Menu1:
-----------------------
File___________Launch
--About_______--Dialog1
--Exit
-----------------------
And I am VERY new to this
------------------------------
I love Yuzu-Chan! =P
Have u eaten ur chicken 2day?
|
|
|
|
|
Hi. I want to enable users to search my web site (Win2k IIS). My database is MS Access 2000. I can connect to it using ASP and query/update/delete. My problem is that I don't know how I could configure my database to search through my site. My solution so far was to copy the content of each page and put it into a seperate table in my database, and then have the ASP page do a SELECT * in the SQL language. The problem is that MS Access only allows 250 characters in each text table so you can see that this won't work. I would like it if some Guru could point me in the right direction with their brilliant ideas. If you provide me with any help I will hold you in the highest regard and kiss the ground three times in your honor. Thank you in advance
|
|
|
|
|
If you use the Memo type instead of Text, you can store up to 65,535 characters.
Jon Sagara
"Ninety percent of baseball is mental, the other half is physical." -- Yogi Bera
|
|
|
|