|
You're right, pesho. I was working off of misinformation. You do know that schools and professors lies to us, right? The only thing I disagree with you is:
pesho2932 wrote:
By the way
"Postfix mode causes the increment to happen after the value of the variable is used in the expression"
means the value is incremented after it is used in x++. Not after all occurances of x in the statement are evaluated.
The quote: "Postfix mode causes the increment to happen after the value of the variable is used in the expression" does not mean that it is incremented after it is used in x++, it means it is incremented after x was used in the expression x + x . However, as I admitted in another post on this thread, the author of that book was most likely teaching about the compiler that was distributed with the book, and apparently it does not apply to all C++ code for all compilers.
However, as an aside, after participating in this thread, I believe that the action of x++ or ++x should have been defined in C++, but as an expert explained, it was "too difficult".
Danny
|
|
|
|
|
funny huh...
I believe they would say "Postfix mode causes the increment to happen after the value of the variable is used in the STATEMENT," if they meant what you suggest.
And if you find funny what I say about the order of evaluation of operands, you should revise your knowledge of sequence points.
...as should do anyone who thinks that y = x + x++; is defined by C++.
|
|
|
|
|
No, the writer would no have used the word statement. The full quote I gave in an earlier post read:
"The difference is important, however, when these operators are used in statements that do more than just incrementing or decrementing. For example, look at the following lines:
num = 4;
cout << num++;
The cout statement above is doing two things: (1) displaying the value of the num, and (2) incrementing the num. But which happens first? cout will display a different value if num is incremented first than if num is incremented last. The answer depends upon the mode of the increment operator.
Postfix mode causes the increment to happen after the value of the variable is used in the expression. In the statement above, cout will display 4 and then num will be incremented to 5."
I notice the writer does use the word statement a couple of times in that quote. The writer then goes on to explain how the postfix operator works the same in mathematical expressions. On the next page he writes, "The increment and decrement operators can also be used on variables in mathematical expressions. Consider the following program segment:
a=2;<br />
b=5;<br />
c=a*b++;<br />
cout << a << " " << b << " " << c;
In the statement c = a * b++ , c is assigned the value of a times b , which is 10. b is then incremented. The cout statement will display:
2 6 10 "
But of course this doesn't cover the case where b , for example, is used twice in the same expression. The instruction leads one to believe that in a statement, for example, such as b * b++ , that as in the above b++ would be incremented after used in the expression b * b . This is how it works on my compiler. Apparently that is not so, but you can see where this instruction, and others, would lead one to believe what I had previously assumed.
pesho2932 wrote:
And if you find funny what I say about the order of evaluation of operands, you should revise your knowledge of sequence points.
My, someone's getting touchy. By the way, weren't you the one that asked the question (well, more of a statment really) that started this thread? And now you say you know all about it?
As to your original question. You posted:
int x = 1;<br />
x = x++;
And then stated:
"I think this should be well defined.
The operations are:
1. ++
2. =
As the expression x++ is an operand of operator=, I think it should be evaluated first (??).
The final value of x should then be 1."
In your original post the question is trivial. It does not matter when x++ executed, since the increment also reassigns the incremented variable back to itself, as you noted, "But the increment seems to occur after the assignment. And x = 2." So who's the one that should revise their knowledge of C++?"
Danny
|
|
|
|
|
bugDanny wrote:
But of course this doesn't cover the case where b , for example, is used twice in the same expression
Indeed.
As an aside, in the statement cout << num++; there are four expressions: cout << num++ , cout , num , and num++ the first one conicides with the whole statement. So I don't think the writer will refer to it several times as the statement and then use the word expression for the same thing.
Here is a quotation from C++ specification:
"It is important to note that a postfix increment or decrement expression evaluates to the value of the expression prior to application of the respective operator. The increment or decrement operation occurs after the operand is evaluated. This issue arises only when the postfix increment or decrement operation occurs in the context of a larger expression."
I think, here, it is clear what is meant by "the expression."
About x = x++; I was not sure and the question was whether you agree. Now I have a better idea thanks to David Crow ("after" does not mean "immediately after"). About y = x + x++; , I had no question. It is clear to me and I gave it as an example in which the behavior is undefined.
The difference between the two is that in the first case x++ is an operand of operator= so it has to be evaluated before operator= . (But I've come to believe that evaluation of x++ can be considered complete before the increment as there is no sequence point and the result of x++ is independent of the increment. So the increment can occur before the assignment or after.)In the second one x and x++ are the two operands of operator+ so it is not known which one the compiler will choose to evaluate first. (And if it chooses to evaluate x++ first the increment will occur at some later point and may be before or after x is evaluated.)
I'm done with this topic.
|
|
|
|
|
pesho2932 wrote:
So I don't think the writer will refer to it several times as the statement and then use the word expression for the same thing.
Except, of course, in the case where x++ is used in an expression that is not a whole statement, like in, y = c + (b * x++); Here the expression that x is used in before it is incremented is obviously (b * x) .
pesho2932 wrote:
I think, here, it is clear what is meant by "the expression."
Ah, yes, "the expression" being what x++ was called in, not x++ itself, as I had noted. ("the expression prior to application of the respective operator.")
pesho2932 wrote:
I'm done with this topic.
Yes, please let the constant repeating of ourselves be over!
Danny
|
|
|
|
|
So be it.
I have no desire to explain c++ specification to you.
|
|
|
|
|
From the ASM code of VC++2003:
int x = 1;
00412F6E mov dword ptr [x],1
int y;
y = 5 + x++;
00412F75 mov eax,dword ptr [x]
00412F78 add eax,5
00412F7B mov dword ptr [y],eax
00412F7E mov ecx,dword ptr [x]
00412F81 add ecx,1
00412F84 mov dword ptr [x],ecx
printf("y = %d\n", y);
00412F87 mov eax,dword ptr [y]
00412F8A push eax
00412F8B push offset string "y = %d\n" (4240E4h)
00412F90 call @ILT+1320(_printf) (41152Dh)
00412F95 add esp,8
x = 1;
00412F98 mov dword ptr [x],1
x = y + x++;
00412F9F mov eax,dword ptr [y]
00412FA2 add eax,dword ptr [x]
00412FA5 mov dword ptr [x],eax
00412FA8 mov ecx,dword ptr [x]
00412FAB add ecx,1
00412FAE mov dword ptr [x],ecx
printf("x = %d\n", x);
00412FB1 mov eax,dword ptr [x]
00412FB4 push eax
00412FB5 push offset string "x = %d\n" (424054h)
00412FBA call @ILT+1320(_printf) (41152Dh)
00412FBF add esp,8
// Old.
int x = 1;
0041262E mov dword ptr [x],1
x = x;
00412635 mov eax,dword ptr [x]
00412638 mov dword ptr [x],eax
x++;
0041263B mov eax,dword ptr [x]
0041263E add eax,1
00412641 mov dword ptr [x],eax
x = x++;
00412644 mov eax,dword ptr [x]
00412647 mov dword ptr [x],eax
0041264A mov ecx,dword ptr [x]
0041264D add ecx,1
00412650 mov dword ptr [x],ecx
Maxwell Chen
-- modified at 22:50 Sunday 18th September, 2005
|
|
|
|
|
Maxwell Chen shows my case quite well. Thank you.
Danny
|
|
|
|
|
Dear fellow coders,
Im just trying to establish if the current user has the privileges to stop/start services.
Im trying to link some example code from the MDSN website into my visual C++ 6.0 project
and i get the following error. can you help?
Linking...
OLMECDlg.obj : error LNK2001: unresolved external symbol "int __cdecl
CheckTokenMembership(void *,void *,int *)"
(?CheckTokenMembership@@YAHPAX0PAH@Z)
Debug/OLMEC.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
the example code i was using was as follows:
#include <winbase.h>
#include <windows.h>
extern BOOL CheckTokenMembership(HANDLE TokenHandle,PSID SidToCheck,PBOOL
IsMember);
/*++
Routine Description: This routine returns TRUE if the caller's process
is a member of the Administrators local group. Caller is NOT expected
to be impersonating anyone and is expected to be able to open its own
process and process token.
Arguments: None.
Return Value:
TRUE - Caller has Administrators local group.
FALSE - Caller does not have Administrators local group. --
*/
BOOL COLMECDlg::IsUserAdmin(VOID)
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup;
b = AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&AdministratorsGroup);
if(b)
{
if (!CheckTokenMembership( NULL, AdministratorsGroup, &b))
{
b = FALSE;
}
FreeSid(AdministratorsGroup);
}
return(b);
}
Please note that I have also setup my linker to include advapi32.lib but
this still fails. What am i doing wrong or is there a mistake on the MSDN
webpage?
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthz/s
ecurity/checktokenmembership.asp)
Im just trying to establish if the current user has the privileges to stop/start services.
Has anyone done this before, or know how to do it in VC6.0 ?
thanks,
Paul
|
|
|
|
|
Rainos wrote:
extern BOOL CheckTokenMembership(HANDLE TokenHandle,PSID SidToCheck,PBOOL
IsMember);
This statement is unnecessary. As long as you have included winbase.h or windows.h , the function prototype already exists.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
But if I do this - it doesnt compile! - Even with winbase.h or windows.h
Do I need to have Platform SDK or a different version of VC++ for this?
|
|
|
|
|
Rainos wrote:
But if I do this - it doesnt compile!
What compiler(s) error do you get?
Rainos wrote:
Do I need to have Platform SDK...for this
It's always a good idea to be using the latest Platform SDK (dated Feb 2003) for your development.
Rainos wrote:
Do I need to have...a different version of VC++ for this?
No, version 6 is fine.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
The compiler error is simply:
error C2065: 'CheckTokenMembership' : undeclared identifier
Dont think I have Platform SDK installed, probably just plain VC++6.0.
Ive ordered a copy of the Feb 2003 version on CD. Can it be downloaded from anywhere?
thanks,
Rainos
|
|
|
|
|
I plugged your IsUserAdmin() funnction into one of my dialog-based applications and it compiled/linked without issue.
Rainos wrote:
Can it be downloaded from anywhere?
Not any more. It was downloadable up until a few months ago.
Good luck.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Thanks for trying that.
Im guessing you have Platform SDK installed then?
If so that confirms that I need it.
thanks,
Rainos
|
|
|
|
|
Rainos wrote:
Im guessing you have Platform SDK installed then?
That is correct.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Hi All,
I've been working on a project that makes use of CTabCtrl in a derived class.
My problem is that when my application runs on Windows XP Themed, the background color of that tabs that should be white (?)- default - are gray as the dialogs that they are. So it causes a strange feeling... the tabs titles are white and beautiful... but the tabs contents are gray...
Does anybody already pass through this or have some idea of how can I avoid this? Maybe makes the color of my dialogs (tab contents) transparent... I just don't know
ps.: its important that works on XP Themed and other versions of Windows too.
Thanks all for the attention,
Cheers!
-- modified at 10:16 Thursday 15th September, 2005
|
|
|
|
|
RickyC wrote:
makes use of CTabCtrl in a derived class.
The default CTabCtrl displays a white background (as expected) on my themed XP system. Perhaps the derived control you're using swaps in modeless dialogs when the user switches tabs? If so, the dialog's background will be COLOR_WINDOW (grey) by default.
Instead of the CTabCtrl derived control, you may want to try using a CPropertySheet (and CPropertyPage s) and see if that gets you the desired behavior.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Hi Ravi, thanks for your answer.
I have been looking into CPropertySheet and the problem that I see, is that CPropertySheet behave like a Modal Dialog isn't?
And my CTabCtrl is inside a Dialog, like part of the controls of that Dialog.
Can I use the CPropertySheet like a normal MFC Control otherwise than a separated Modal Dialog?
Well, I'll try'it anyway
Thank you very much for your patience and assistance.
|
|
|
|
|
Can I use the CPropertySheet like a normal MFC Control
Yes, absolutely! See this[^] article for instructions.
/ravi
My new year's resolution: 2048 x 1536
Home | Music | Articles | Freeware | Trips
ravib(at)ravib(dot)com
|
|
|
|
|
Thanks Ravi!
You help me a lot!
|
|
|
|
|
|
Hi,
If I'm implementing a view that recieves WM_COMMAND messages, and OnUpdateCmdUIs, what is the easiest way to include some of my view's child windows in the routing, so that they can also recieve these notifications, without reflecting each notification one by one?
Joel Holdsworth
|
|
|
|
|
It's a bit hacky but I often override OnCmdMsg() and forward it on to the child windows to see if any of them want it.
The two most common elements in the universe are Hydrogen and stupidity. - Harlan Ellison
Awasu 2.1.2 [^]: A free RSS reader with support for Code Project.
|
|
|
|
|
Thanks a lot. Worked like a charm!
Joel Holdsworth
|
|
|
|
|