Jump to content
  • Advertisement
Sign in to follow this  
Pharazon

How well you know your c++?

This topic is 5117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Lets see if everyone here at the GDNET can figure out why these c++ questions return the way they do: Try answering the following questions: Why does the following code segment display "equal"? short x = -32768; short y = -x; if (x == y) cout << "equal"; 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"; For integer values of x, (i) When does (x == x == x) evaluate to true? (ii) When does (x != x != x) evaluate to true? (iii) Why is it that (4 >= 3 >= 2) evaluates to false but (2 >= 1 >= 0) evaluates to true? Post your answers. If you don't know, I'll put up answers in a couple days.

Share this post


Link to post
Share on other sites
Advertisement
Off the top of my head

1) overflow on negation
2) 0.01 cannot be represented exactly in binary
3) x=1, never (NaN if x were a float), left-associativity and bool to int promotion rules.

Share this post


Link to post
Share on other sites
Nah, I don't think so. He's been around long enough to know what happens to people who ask homework questions... [evil]

Share this post


Link to post
Share on other sites
1. What does this do innocent code do:


#include <iostream>

void foobar() {
std::cout << "HELLO\n";
}

int main() {

typedef void (&foo)(); //<--- what do you think this is? hint: no its not a pointer to function.

typedef const foo const_foo;

const_foo f = foobar;

f();

return 0;

}


2. Why can't we have this for type members but only for functions? i would love to know the answer to that.

Share this post


Link to post
Share on other sites
1) Function reference type. f() should call foobar()
2) Do you mean member typedefs or member functions?

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
1) Function reference type. f() should call foobar()


Hooray, yes a reference to a function, give this man a gold star. [smile]

Quote:
Original post by Fruny
2) Do you mean member typedefs or member functions?


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Off the top of my head, please correct any mistakes.

1) -32768 as a short in binary is
1000000000000000
and negating that is the same as the 2's complement + 1 so we get
0111111111111111 + 1
1000000000000000
2) you're comparing float's to doubles. 0.01 is uses some of the added accuracy of a double, while 0.05 doesn't.

3i) x == 1
3ii) when x != 0
3ii) order of operations and comparisons return 0 or 1, so
4 >= 3 >= 2 becomes
1 >= 2 which is false
while
2>= 1 >= 0
1 >= 0 which is true

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!