Last active
August 8, 2018 04:15
-
-
Save DomNomNom/af9da065a82dcf504cb1df48f88473a6 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 one is coded in a more Object Oriented fashion
This file contains hidden or 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> | |
// 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 getValue() { return private_; } | |
private: | |
int private_; | |
}; | |
const int NOT_READY_YET = -1; | |
class BigAnswerComputer : public X { | |
public: | |
BigAnswerComputer() | |
: answer_to_life_universe_and_everything(NOT_READY_YET) {} | |
// Uses an enormous supercomputer to find The Answer to the Ultimate | |
// Question of Life, the Universe, and Everything. | |
void computeAnswer() { | |
answer_to_life_universe_and_everything = 42; | |
} | |
int getCachedAnswer() { | |
return answer_to_life_universe_and_everything; | |
} | |
private: | |
int answer_to_life_universe_and_everything; | |
}; | |
class BigQuestionComputer : public BigAnswerComputer { | |
public: | |
BigQuestionComputer() | |
: question_to_life_universe_and_everything(NOT_READY_YET) {} | |
void computeQuestion() { | |
// TODO: Implement this once we can compute the BigAnswer with this. | |
} | |
int getCachedQuestion() { | |
return question_to_life_universe_and_everything; | |
} | |
private: | |
int question_to_life_universe_and_everything; | |
}; | |
// Compute the answer_to_life_universe_and_everything and check that it is | |
// correct. Answers is will be contained (cached) in @computers. | |
// We check for errors by computing @n times and testing whether they | |
// all come out the same. | |
// Returns 0 when there are no errors. | |
int compute_answer_and_check_for_errors(BigAnswerComputer* computers, int n) { | |
for (int i=0; i<n; ++i) { | |
computers[i].computeAnswer(); | |
} | |
// Check for errors: All computers should be ready and give the same answer. | |
int num_errors = 0; | |
if (n > 0 && computers[0].getCachedAnswer() == NOT_READY_YET) { | |
++num_errors; | |
} | |
for (int i=1; i<n; ++i) { | |
if (computers[i].getCachedAnswer() != computers[0].getCachedAnswer()) { | |
++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; | |
BigQuestionComputer 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].getValue(); // 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