Created
April 27, 2011 02:00
-
-
Save JoshCheek/943594 to your computer and use it in GitHub Desktop.
Why "pass by value" and "pass by reference" are meaningless phrases
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
/* The problem with phrases like "pass by value", "pass by object reference", and "pass by reference" | |
* is that they are utterly meaningless becaues there are three perspectives one can take when trying | |
* to classify these things. | |
*/ | |
typedef struct { int value; } Object; | |
/* PERSPECTIVE 1: The parameters of the function being called (this is the one that _should_ matter) */ | |
void function_by_value (Object o) { } | |
void function_by_object_reference (Object* o) { } | |
void function_by_reference (Object** o) { } | |
void main() { | |
/* PERSPECTIVE 2: The arguments that will be passed */ | |
Object variable_by_value = {0}; | |
Object* variable_by_object_reference = &variable_by_value; | |
Object** variable_by_reference = &variable_by_object_reference; | |
/* PERSPECTIVE 3: the referencing and dereferencing while passing (the least relevant of the three) | |
pass by object reference doesn't have any meaning here */ | |
function_by_value( variable_by_value ); // pass by value | |
function_by_value( *variable_by_object_reference ); // no name for this | |
function_by_value( **variable_by_reference ); // no name for this | |
function_by_object_reference( &variable_by_value ); // pass by reference | |
function_by_object_reference( variable_by_object_reference ); // pass by value (Ruby and Java do this) | |
function_by_object_reference( *variable_by_reference ); // no name for this | |
function_by_reference( &&variable_by_value ); // not possible | |
function_by_reference( &variable_by_object_reference ); // pass by reference | |
function_by_reference( variable_by_reference ); // pass by value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment