Created
July 26, 2013 16:06
-
-
Save jpmec/6090090 to your computer and use it in GitHub Desktop.
Test the C++ keyword mutable
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
/** A set of tests for the C++ keyword mutable. | |
* | |
* Compile with: | |
* g++ -Wall test_cpp_mutable.cpp -o test_cpp_mutable | |
*/ | |
#include <iostream> | |
#include <cassert> | |
using namespace std; | |
/// A simple structure to demonstrate the mutable keyword. | |
class SimpleStruct | |
{ | |
public: | |
SimpleStruct(void) | |
: mutable_value_(0), value_(0) | |
{ | |
// default constructor | |
} | |
unsigned GetValue(void) const | |
{ | |
return value_; | |
} | |
void SetValue(unsigned value) | |
{ | |
value_ = value; | |
} | |
unsigned GetMutableValue(void) const | |
{ | |
return mutable_value_; | |
} | |
/// Note that this function is declared as a const function, | |
/// but we can still modify the mutable value. | |
void SetMutableValue(unsigned value) const | |
{ | |
mutable_value_ = value; | |
} | |
private: | |
mutable unsigned mutable_value_; ///< Mutable member, can never be const | |
unsigned value_; ///< Normal member, can be const | |
}; | |
static SimpleStruct static_global_simple_struct_instance; | |
static const SimpleStruct static_const_global_simple_struct_instance = SimpleStruct(); | |
int main(int argc, const char* argv[]) | |
{ | |
{ | |
static_global_simple_struct_instance.SetValue(1); | |
assert(1 == static_global_simple_struct_instance.GetValue()); | |
static_global_simple_struct_instance.SetMutableValue(1); | |
assert(1 == static_global_simple_struct_instance.GetMutableValue()); | |
} | |
{ | |
// The compiler will not allow the calls below because | |
// we cannot call a non-const function on a const instance | |
// static_const_global_simple_struct_instance.SetValue(1); | |
static_const_global_simple_struct_instance.SetMutableValue(1); | |
assert(1 == static_const_global_simple_struct_instance.GetMutableValue()); | |
} | |
{ | |
SimpleStruct local_simple_struct_instance; | |
local_simple_struct_instance.SetValue(1); | |
assert(1 == static_global_simple_struct_instance.GetValue()); | |
local_simple_struct_instance.SetMutableValue(1); | |
assert(1 == static_global_simple_struct_instance.GetMutableValue()); | |
} | |
{ | |
const SimpleStruct const_local_simple_struct_instance; | |
// The compiler will not allow the calls below because | |
// we cannot call a non-const function on a const instance | |
// const_local_simple_struct_instance.SetValue(1); | |
const_local_simple_struct_instance.SetMutableValue(1); | |
assert(1 == const_local_simple_struct_instance.GetMutableValue()); | |
} | |
cout << endl << "Tests passed for " << __FILE__ << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment