How well you know your c++?

Started by
50 comments, last by Enigma 19 years, 6 months ago
Quote:Original post by Fruny
Quote:Original post by snk_kid
i mean't instead of pointer to member functions or pointer to data members, why no reference to member functions or reference to data members but you can with free/class functions.


Hmmm. Good question. I didn't even know you could not.

i was stuck with that recently.
thread
additional info

I'm a Pascal programmer swiched to C++ not so long ago, yet i have no troubles answering such questions.

BTW :
"
Why does the following code segment display
"x does not equal 0.01, y equals 0.05"?
float x = 0.01;
float y = 0.05;
if (x != 0.01) cout << "x does not equal 0.01, ";
if (y == 0.05) cout << "y equals 0.05";
"

probably there should be 0.5 , not 0.05
0.05 can't be exactly represented by float nor double. When comparing, IIRC on x68 float is converted to double. IIRC by standard, it's double must be converted to float, but i'm not 100% sure.
Advertisement
to Eriond:

4.5 Integral promotions
An rvalue of type bool can be converted to an rvalue of type int, with false becoming zero and true
becoming one.

to Cocalus:

Isn't the 2's complement just a binary not followed by +1 ?
Quote:Original post by Eriond
Will 2 >= 1 always evaluate to 1 and not some other number like 51?


Yes. In C++ any non-zero value when intepreted as a boolean expression becomes "true" which basically is "1".
Quote:Original post by Dmytry
BTW :
"
Why does the following code segment display
"x does not equal 0.01, y equals 0.05"?
float x = 0.01;
float y = 0.05;
if (x != 0.01) cout << "x does not equal 0.01, ";
if (y == 0.05) cout << "y equals 0.05";
"

probably there should be 0.5 , not 0.05
0.05 can't be exactly represented by float nor double. When comparing, IIRC on x68 float is converted to double. IIRC by standard, it's double must be converted to float, but i'm not 100% sure.


I tried the 0.05 version, the test fails. For 0.5 it works as Dmytry said.

BTW nice thread, thx for the idea.
Quote:Original post by Eriond
Will 2 >= 1 always evaluate to 1 and not some other number like 51?


Yes
daerid@gmail.com
Oh, come on. If you're going to ask a question like "How well you know your c++?", you should at least post problems specific to C++. All of those problems can happen in any number of languages.

The first two are tied to the design of the CPU (i.e. 2's complement negative integers and IEEE floating point numbers). The last question is simple boolean algebra, and again is not specific to any language (other than the fact that some languages will evaluate from left-to-right and others from right-to-left).

The reason I asked was because a friend said some compilers would use another value, which could make 4 >= 3 >= 2 evaluate to true. Thanks for the answer.
The C++ standard says that true has to be converted to 1, nothing else. If the compiler behaves differently it isn't standard conforming.
Quote:
Post your answers. If you don't know, I'll put up answers in a couple days.


How almost insulting...
Quote:
to Cocalus:

Isn't the 2's complement just a binary not followed by +1 ?


Mybad, 2's complement is inverting all the bits (I think that's called the one's complement) then adding one. I remember concepts well but not names of things.

-Cocalus

This topic is closed to new replies.

Advertisement