Created
February 8, 2009 05:21
-
-
Save scooby/60204 to your computer and use it in GitHub Desktop.
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
//Const Quick-Ref | |
int variable_integer = 39; | |
const int constant_integer = 42; | |
const int * var_ptr_to_const_int = &constant_integer; | |
int const * also_var_ptr_to_const_int = &constant_integer; | |
int * const const_ptr_to_var_int = &variable_integer; | |
int const * const const_ptr_to_const_int = &constant_integer; | |
int & ref_to_int = variable_integer; | |
const int & ref_to_const_int = constant_integer; | |
int const & also_ref_to_const_int = constant_integer; | |
// error: ‘const’ qualifiers cannot be applied to ‘int&’ | |
// int & const const_ref_to_var_int = variable_integer; | |
// error: ‘const’ qualifiers cannot be applied to ‘const int&’ | |
//int const & const const_ref_to_const_int = constant_integer; | |
int return_int() { return variable_integer; } | |
const int return_const_int() { return constant_integer; } | |
const int * return_ptr_to_const_int() { return var_ptr_to_const_int; } | |
int const * also_return_ptr_to_const_int() { return also_var_ptr_to_const_int; } | |
int * const return_const_ptr_to_var_int() { return const_ptr_to_var_int; } | |
int const * const return_const_ptr_to_const_int() { return const_ptr_to_const_int; } | |
const int & return_ref_to_const_int() { return constant_integer; } | |
int const & also_return_ref_to_const_int() { return constant_integer; } | |
struct big_t { | |
big_t(int value) : stuff(value) { } | |
void setter_method(int newval) { stuff = newval; } | |
int getter_method() const { return stuff; } | |
int stuff; | |
}; | |
int func_copy( int read_by_cpy) { return read_by_cpy; } | |
void func_edit_by_ref( int & edit_by_ref) { edit_by_ref = 42; } | |
void func_edit_by_ref_mem( big_t & edit_by_ref) { edit_by_ref.stuff = 42; } | |
int func_read_const_ref( int const & read_by_ref) { return read_by_ref; } | |
int func_read_const_ref_mem(big_t const & read_by_ref) { return read_by_ref.stuff; } | |
void func_edit_by_ptr( int * edit_by_ptr) { *edit_by_ptr = 42; } | |
void func_edit_by_ptr_mem( big_t * edit_by_ptr) { edit_by_ptr->stuff = 42; } | |
int func_read_const_ptr( int const * read_by_ptr) { return *read_by_ptr; } | |
int func_read_const_ptr_mem(big_t const * read_by_ptr) { return read_by_ptr->stuff; } | |
const big_t constant_big(constant_integer); | |
big_t variable_big(variable_integer); | |
int main(void) { | |
variable_integer = 56; | |
// error: assignment of read-only variable ‘constant_integer’ | |
// constant_integer = 77; | |
// error: invalid lvalue in unary ‘&’ | |
// var_ptr_to_const_int = &33; | |
var_ptr_to_const_int = &variable_integer; | |
also_var_ptr_to_const_int = &variable_integer; | |
*const_ptr_to_var_int = 22; | |
// error: assignment of read-only location | |
// *const_ptr_to_const_int = 26; | |
func_copy(variable_integer); | |
func_copy(constant_integer); | |
func_edit_by_ref(variable_integer); | |
// error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ | |
// func_edit_by_ref(constant_integer); | |
// error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ | |
// func_edit_by_ref(ref_to_const_int); | |
// error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ | |
// func_edit_by_ref(*var_ptr_to_const_int); | |
func_edit_by_ref(*const_ptr_to_var_int); | |
// error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ | |
// func_edit_by_ref(*const_ptr_to_const_int); | |
func_edit_by_ref_mem(variable_big); | |
// error: invalid initialization of reference of type ‘big_t&’ from expression of type ‘const big_t’ | |
// func_edit_by_ref_mem(constant_big); | |
func_read_const_ref_mem(variable_big); | |
func_read_const_ref_mem(constant_big); | |
func_read_const_ptr(&variable_integer); | |
func_read_const_ptr(&constant_integer); | |
func_edit_by_ptr(&variable_integer); | |
// error: invalid conversion from ‘const int*’ to ‘int*’ | |
// func_edit_by_ptr(&constant_integer); | |
// error: invalid conversion from ‘const int*’ to ‘int*’ | |
// func_edit_by_ptr(var_ptr_to_const_int); | |
func_edit_by_ptr(const_ptr_to_var_int); | |
func_edit_by_ptr(const_ptr_to_var_int); | |
// invalid conversion from ‘const int* const’ to ‘int*’ | |
// func_edit_by_ptr(const_ptr_to_const_int); | |
func_edit_by_ptr_mem(&variable_big); | |
// error: invalid conversion from ‘const big_t*’ to ‘big_t*’ | |
// func_edit_by_ptr_mem(&constant_big); | |
variable_big.setter_method(11); | |
// error: passing ‘const big_t’ as ‘this’ argument of ‘void big_t::setter_method(int)’ discards qualifiers | |
// constant_big.setter_method(12); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment