Skip to content

Instantly share code, notes, and snippets.

@ylyhlh
Created June 8, 2014 13:48
Show Gist options
  • Save ylyhlh/e596cf0b8518ee91db52 to your computer and use it in GitHub Desktop.
Save ylyhlh/e596cf0b8518ee91db52 to your computer and use it in GitHub Desktop.
Testing the use of conceptional constance.
#include <iostream>
#include "MoneyChanger.h"
//Testing conceptional constance with 'mutable' keyword
class testClass {
/* make it mutable then callable by const fucntion.
* It's better to make memberfunction called by this class
* be const function. In that way you do not need mutable
* funciton here. But some time you cannot modify it,
* for example you are using prebuild library */
mutable MoneyChanger mc;
public:
// you must use a user define constructor, when you construct a const object.
testClass() {}
const long long test(double dollars) const{
return mc.howManyCombinations(dollars);
}
};
int main(int argc, const char * argv[])
{
// this is conceptional constance instead of bit-wise constance.
const testClass normalTestObj;
std::cout << normalTestObj.test(100) << "\n";
// next line will be very fast, because last line do all computation
// needed and save results into mutable member.
std::cout << normalTestObj.test(10) << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment