Solved

Simple syntax question

Posted on 2004-08-02
5
227 Views
Last Modified: 2010-04-01
Please describe what this does:

byIndex = (m_csBitmaps[2].hBitmap == NULL ? 0 : 2);

Is it some kind of "if then else" shorthand?
0
Comment
Question by:PMH4514
  • 3
5 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11697780
Yes, it is some kind of if...then...else

value = condition ? return_for_true : return_for_false;

It is a common operator in many languages.
0
 
LVL 22

Expert Comment

by:grg99
ID: 11698375
It's a bit of if/the/else shorthand for expressions.  A bit cryptic.

In this case it says:  if the .hBitMap is NULL, this side of the equals sign has a "0", otherwise it has a "2".

In other less cryptic (altho dead) languiages like Algol, youd say the same thing in a much less head-scratching form:


byIndex :=  if m_csBitmaps[2].hBitmap = NULL  then 0  else 2;

0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 25 total points
ID: 11698445
Equivalent C expression:

if (m_csBitmaps[2].hBitmap == NULL)
      byIndex = 0;
else
      byIndex = 2;

Shorter expression:

if (!m_csBitmaps[2].hBitmap)
      byIndex = 0;
else
      byIndex = 2;
0
 

Author Comment

by:PMH4514
ID: 11698517
thanks Jamie.. I was pretty sure of that.. I've seen it before, I tend to write more verbose code.. that's going to change now that I'm doing lower level stuff.. I wonder if the shorthand is any faster once compiled? (if so it's probably not even perceptable even with timer scripts)

0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11698541
> I wonder if the shorthand is any faster once compiled?
That is a compiler-dependent feature, if you are very interested, you have to some a performance test by yourself.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now