Last active
August 7, 2018 01:24
-
-
Save DomNomNom/1da117b3e226256ff4e17e6f071e1ab5 to your computer and use it in GitHub Desktop.
Accidentally breaks C++ Access Rights while computing The Answer to the Ultimate Question of Life, the Universe, and Everything.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// File x.h | |
// This class was named X to be consistent with http://www.gotw.ca/gotw/076.htm | |
// | |
// Its primary purpose is to hold a private value which no one should be able | |
// to accidentally modify. | |
class X | |
{ | |
public: | |
X() : private_(1) { /*...*/ } | |
int Value() { return private_; } | |
private: | |
int private_; | |
}; | |
// File Murphy42.cpp | |
const int NOT_READY_YET = -1; | |
class BigAnswer : public X | |
{ | |
public: | |
BigAnswer() : answer_to_life_universe_and_everything(NOT_READY_YET) {} | |
int answer_to_life_universe_and_everything; | |
}; | |
class BigQuestion : public BigAnswer | |
{ | |
public: | |
BigQuestion() : question_to_life_universe_and_everything(NOT_READY_YET) {} | |
int question_to_life_universe_and_everything; | |
}; | |
// Uses an enormous supercomputer to find The Answer to the Ultimate Question of | |
// Life, the Universe, and Everything. | |
int compute_answer() { | |
return 42; | |
} | |
// A c-style function to compute the answer_to_life_universe_and_everything and | |
// to check that it is correct. Output is written to @answers. | |
// We check for errors by computing @num_answers times and testing whether they | |
// all come out the same. | |
// Returns 0 when there are no errors. | |
int compute_answer_and_check_for_errors(BigAnswer* answers, int num_answers) { | |
for (int i=0; i<num_answers; ++i) { | |
answers[i].answer_to_life_universe_and_everything = compute_answer(); | |
} | |
// Check for errors (consistency) | |
int num_errors = 0; | |
for (int i=1; i<num_answers; ++i) { | |
if ( | |
answers[i].answer_to_life_universe_and_everything != | |
answers[0].answer_to_life_universe_and_everything | |
) { | |
++num_errors; | |
} | |
} | |
return num_errors; | |
} | |
int main() { | |
// We intend to compute the question_to_life_universe_and_everything with | |
// these in the near future. | |
const int num_error_checks = 10; | |
BigQuestion earths[num_error_checks]; | |
// Before computing the question_to_life_universe_and_everything, | |
// let's first check that we can compute the answer correctly. | |
int error = compute_answer_and_check_for_errors(earths, num_error_checks); | |
if (error) { | |
std::cout << "oh no! " << error << std::endl; | |
return error; | |
} | |
std::cout << "Answer computed and checked successfully!" << std::endl; | |
// Print out the result of one. We should be able to choose any earth | |
// since compute_answer_and_check_for_errors has verified that all the | |
// outputs are the same. | |
std::cout << "And the answer is: "; | |
std::cout << earths[1].Value(); // WAIT, WHAT?!? This is 42. But how? | |
std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment