|
coding style issues, which better, how used ??
1. if (true == v1)
{
v2 = 0;
}
else
{
v2 = 1;
}
2. v2 ((true == v1) ? 0 : 1);
|
|
|
|
|
Don't compare a boolean value with true or false . There's no logic in that (sic!). It'll cause more confusion than clarity. For instance:
if(((enabled == true) == true) == true...) Where dou you stop? It's obviously clearer with
if(enabled)
But, as the question is formulated, me thinks that
v2 = v1 ? 0 : 1; is best.
--
The Blog: Bits and Pieces
|
|
|
|
|
I just want to add, it can also be a very buggy thing to do. Even a C++ 'bool' can have more values than just true and false.
But aside from that, if you are working with Windows API, comparing to TRUE is the wrong thing to do since the API are defined as being 0 and != 0 which means that they might return 5219 for TRUE.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Hello,
It's just a matter of preference. You can also write:
v2 = !v1;
Since you can use the opposite value of v1, you can omit v2. This can go on endlessly. Choose what comforts you most.
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
Bob Stanneveld wrote:
It's just a matter of preference. You can also write:
v2 = !v1;
Yes, when v2 is also a bool , but I sensed that the question covered the generic case.
--
The Blog: Bits and Pieces
|
|
|
|
|
Johann Gerell wrote:
Yes, when v2 is also a bool
If v2 is an int , true will be changed to 1 and false to 0. Unless v2 is an user defined type, this can work...
Behind every great black man...
... is the police. - Conspiracy brother
Blog[^]
|
|
|
|
|
if (v1) {
v2 = 0;
}
else {
v2 = 1;
} or
v2 = v1 ? 0 : 1; For myself, I only use the condtional operator (? : ) when the result is a simple expression.
Software Zen: delete this;
|
|
|
|
|
if(v1) { v2 = 0; }
else { v2 = 1; } runs a little faster than
v2 = v1 ? 0 : 1;
Maxwell Chen
|
|
|
|
|
Maxwell Chen wrote:
if(v1) { v2 = 0; }
else { v2 = 1; }
runs a little faster than
v2 = v1 ? 0 : 1;
How thats run Faster?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
About four years ago I did some test about your question with VC++6. Just now I did again with VC++7.1. My method is as:
void Test_If(bool& v1, int& v2)
{
if(v1) {
v2 = 0;
}
else {
v2 = 1;
}
}
void Test_Op(bool& v1, int& v2)
{
v2 = v1 ? 0 : 1;
}
void CTestComboDlg::OnBnClickedButton1()
{
bool v1 = true;
int v2 = 3;
int i;
DWORD ds, de;
ds = GetTickCount();
for(i = 0; i < 0x10000000; i++) {
Test_If(v1, v2);
}
de = GetTickCount();
TRACE("IF: %u \n", de - ds);
ds = GetTickCount();
for(i = 0; i < 0x10000000; i++) {
Test_Op(v1, v2);
}
de = GetTickCount();
TRACE("OP: %u \n", de - ds);
} The output is:
IF: 48690
OP: 52576
Maxwell Chen
|
|
|
|
|
Maxwell Chen wrote:
he output is:
IF: 48690
OP: 52576
Nice, Thanks for same
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
ThatsAlok wrote:
How thats run Faster?
maybe because it has to evaluate the ?: operator before.
1. using if :
- evaluate the condition.
- test the if statement
- assign v2
2. using ?: :
- enters the = operator
- enters the ?: operator
- evaluate the condition
- return the right parameter wether the condition is true or false
- assign v2
i feel it like this... but i personnally still use the second case
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
You should not have to feel a thing.
Just DO IT by examining the disassembly.
The assembly dump would explain why one is faster than the other.
|
|
|
|
|
In my dialog based program the Mainmenus underscore symbols not appearing by default. If I pressed alt key then only they are apearing. I want to make my menu should show shortcut keys like VC++ editor shows. with out pressing alt key I want to show shortcut keys. Any help please. My OS is XP.
Nice talking to you.
|
|
|
|
|
Maybe you forgot to place the ampersand before the character that you want to be underlined
|
|
|
|
|
Sorry, I kept it. My application is working fine in other os. like Windows - 2000, NT. But not in XP.
Nice talking to you.
|
|
|
|
|
G Haranadh wrote:
If I pressed alt key then only they are apearing
That's the default behavior in XP. You can change it in Control Panel - Display - Appearance - Effects - Hide underlined letters for keyboard navigation until I press the Alt key.
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
The actual problem is I could not satisfy all clients by saying, disable this behavior. I have to disable programatically particularly to my application. It should not effects other applications. Can we do this? Thank you for replying.
Nice talking to you.
|
|
|
|
|
G Haranadh wrote:
The actual problem is I could not satisfy all clients by saying, disable this behavior.
Why not? It's a system setting, and applications are expected to respect those settings, not to override them. The same goes for fonts, colors, sizes, etc.
I can understand some client may want to always see the underlines, but then he probably also want to see them in all the applications he use, so changing it at the system level is what he needs.
What am I missing?
That being said...
G Haranadh wrote:
Can we do this?
It may be possible, but I don't know how or at what cost...
G Haranadh wrote:
Nice talking to you
Same here.
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
CStrings are used to declare string, right. But what if I want to check for the individual contents of the string. In Turbo C, strings are declared as char myString[10]; wherein you can access every single character in the string by ch = myString[5];
How will I going to be able to do it in Visual C++? coz if I used CString as a variable type, myString[5] is an error.
|
|
|
|
|
CString has an operator[] to access individual characters, although it is read-only.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | 1ClickPicGrabber | NEW~! CP SearchBar v3.0 | C++ Forum FAQ
"Just because the box has 2 gigabytes of memory doesn't mean you get to use it all!"
-- Rico Mariani, CLR perf guy
|
|
|
|
|
thanx,.,. ok, then do i have to use char myString[max] or is there another variable type that I could use?
|
|
|
|
|
You can use the member function CString as GetAt().
For eg.,
CString strName;
strName.GetAt(0);
strName.GetAt(1);
....
....
....
You can use GetLength() function of CString and loop through all the characters.
Regards,
Neelesh K J Jain.
Don't Forget to Rate the replies when they help you.
|
|
|
|
|
Is strName.GetAt(1) a read-only or I can also assign characters to them?
|
|
|
|
|
There's a corresponding SetAt function.
CString members
He is smart. He will make our Windows go.
|
|
|
|