På yggenyk.dk bruger vi cookies til at give dig en god oplevelse og til at indsamle statistik, der kan være med til at forbedre brugeroplevelsen. Hvis du klikker på et link på yggenyk.dk, accepterer du samtidig vores cookiepolitik.
References, Pointers and Constants: Difference between revisions
(16 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
[[Category:English pages]] | |||
[[Category:Software development]] | |||
==Simple C++ Data Types== | ==Simple C++ Data Types== | ||
{|width="100%" border="1" cellpadding="2" cellspacing="0" | {|width="100%" border="1" cellpadding="2" cellspacing="0" | ||
|- | |- valign="top" | ||
!style="color:# | !style="color:#2277dd; width:120px"|C++ code | ||
!style="color:# | !style="color:#2277dd"|What it means | ||
!style="color:# | !style="color:#2277dd"|When to use it | ||
|- | |- valign="top" | ||
| | | | ||
char<br> | ''char''<br> | ||
int<br> | ''int''<br>'' | ||
double | ''double'' | ||
| | | | ||
Single (scalar) value (character, integer, floating point number) | Single (scalar) value (character, integer, floating point number) | ||
Line 15: | Line 17: | ||
To store data<br> | To store data<br> | ||
To pass data to a function by value<br> | To pass data to a function by value<br> | ||
To return data by value | To return data by value | ||
|- | |- valign="top" | ||
| | | | ||
const char<br> | ''const char''<br> | ||
const int<br> | ''const int''<br> | ||
const double | ''const double''<br> | ||
| | | | ||
Constant value | Constant value | ||
Line 26: | Line 28: | ||
To declare a constant -- a value that cannot be changed<br> | To declare a constant -- a value that cannot be changed<br> | ||
Has no effect for passing values to a function or from returning values from a function | Has no effect for passing values to a function or from returning values from a function | ||
|- | |- valign="top" | ||
| | | | ||
char []<br> | ''char []''<br> | ||
int []<br> | ''int []''<br> | ||
double [] | ''double []'' | ||
| | | | ||
Array (vector) of values<br> | Array (''vector'') of values<br> | ||
(char [] is a string) | (''char []'' is a string) | ||
| | | | ||
To store the same type of data together<br> | To store the same type of data together<br> | ||
To pass an array to a function | To pass an array to a function | ||
|- | |- valign="top" | ||
| | | | ||
const char []<br> | ''const char []''<br> | ||
const int []<br> | ''const int []''<br> | ||
const double [] | ''const double []'' | ||
| | | | ||
Constant array of values<br> | Constant array of values<br> | ||
(const char [] is a string constant) | (''const char []'' is a string constant) | ||
| | | | ||
To declare an array of constant values (no element in the array can be changed)<br> | To declare an array of constant values (no element in the array can be changed)<br> | ||
const char [] is used to declare a string constant | ''const char []'' is used to declare a string constant | ||
|- | |- valign="top" | ||
| | | | ||
char *<br> | ''char *''<br> | ||
int *<br> | ''int *''<br> | ||
double * | ''double *'' | ||
| | | | ||
Pointer to a single value or an array of values | Pointer to a single value or an array of values | ||
| | | | ||
To store a string (char *)<br> | To store a string (''char *'')<br> | ||
To store an array of values when you have to allocate the memory dynamically (using new)<br> | To store an array of values when you have to allocate the memory dynamically (using ''new'')<br> | ||
To pass an array to a function<br> | To pass an array to a function<br> | ||
To pass a value to a function which the function will set (passing by reference)<br> | To pass a value to a function which the function will set ('''passing by reference''')<br> | ||
To return a dynamically-allocated array from a function.<br> | To return a dynamically-allocated array from a function.<br> | ||
|- | |- valign="top" | ||
| | | | ||
const char *<br> | ''const char *''<br> | ||
const int *<br> | ''const int *''<br> | ||
const double * | ''const double *'' | ||
| | | | ||
Pointer to a constant value | Pointer to a constant value | ||
Line 72: | Line 74: | ||
To return a pointer to a constant value<br> | To return a pointer to a constant value<br> | ||
To pass a pointer to a constant value to a function | To pass a pointer to a constant value to a function | ||
|- | |- valign="top" | ||
| | | | ||
char &<br> | ''char &''<br> | ||
int &<br> | ''int &''<br> | ||
double & | ''double &'' | ||
| | | | ||
Reference to a value | Reference to a value | ||
| | | | ||
To pass a value to a function which will be set by the function (without using a pointer) | To pass a value to a function which will be set by the function (without using a pointer) | ||
|- | |- valign="top" | ||
| | | | ||
char * &<br> | ''char * &''<br> | ||
int * &<br> | ''int * &''<br> | ||
double * & | ''double * &'' | ||
| | | | ||
Reference to a pointer | Reference to a pointer | ||
| | | | ||
To pass a pointer to a function where the function will set the pointer (either by calling new or by assigning to the pointer | To pass a pointer to a function where the function will set the pointer (either by calling ''new'' or by assigning to the pointer | ||
|- | |- valign="top" | ||
| | | | ||
char * const<br> | ''char * const''<br> | ||
int * const<br> | ''int * const''<br> | ||
double * const | ''double * const'' | ||
| | | | ||
Constant pointer | Constant pointer | ||
Line 100: | Line 102: | ||
When you want to point to a specific value. Allows to you to change the data that is pointed to, but you can't change the pointer | When you want to point to a specific value. Allows to you to change the data that is pointed to, but you can't change the pointer | ||
|} | |} | ||
==C++Structures== | ==C++Structures== | ||
{|width="100%" border="1" cellpadding="2" cellspacing="0" | {|width="100%" border="1" cellpadding="2" cellspacing="0" | ||
|- | |- valign="top" | ||
!style="color:# | !style="color:#2277dd; width:150px"|C++ code | ||
!style="color:# | !style="color:#2277dd"|What it means | ||
!style="color:# | !style="color:#2277dd"|When to use it | ||
|- | |- valign="top" | ||
| | | | ||
struct somestruct<br> | ''struct somestruct''<br> | ||
{<br> | ''{''<br> | ||
. . .<br> | ''. . .''<br> | ||
}; | ''};'' | ||
| | | | ||
Data structure (user-defined) | Data structure (user-defined) | ||
| | | | ||
To store different types of data together | To store different types of data together | ||
|- | |- valign="top" | ||
| | | | ||
struct somestruct<br> | ''struct somestruct''<br> | ||
(struct is optional) | (''struct'' is optional) | ||
| | | | ||
Data structure value | Data structure value | ||
Line 126: | Line 129: | ||
To store values in a data structure<br> | To store values in a data structure<br> | ||
'''Not''' to be used to pass structures to a function or to return structures from a function (inefficient!) | '''Not''' to be used to pass structures to a function or to return structures from a function (inefficient!) | ||
|- | |- valign="top" | ||
| | | | ||
const struct somestruct | ''const struct somestruct'' | ||
| | | | ||
Constant data structure value | Constant data structure value | ||
| | | | ||
When you don't want of the member data values to be changed | When you don't want of the member data values to be changed | ||
|- | |- valign="top" | ||
| | | | ||
struct somestruct & | ''struct somestruct &'' | ||
| | | | ||
Reference to a data structure value | Reference to a data structure value | ||
| | | | ||
To pass a data structure to a function where the function will change some data members of that structure | To pass a data structure to a function where the function will change some data members of that structure | ||
|- | |- valign="top" | ||
| | | | ||
const struct somestruct & | ''const struct somestruct &'' | ||
| | | | ||
Constant reference to a data structure value | Constant reference to a data structure value | ||
| | | | ||
To pass a data structure to a function where the function will not or cannot change any data members of that structure | To pass a data structure to a function where the function will not or cannot change any data members of that structure | ||
|- | |- valign="top" | ||
| | | | ||
struct somestruct [] | ''struct somestruct []'' | ||
| | | | ||
Array of data structure values | Array of data structure values | ||
| | | | ||
Used to group several identical data structures (with different values) together | Used to group several identical data structures (with different values) together | ||
|- | |- valign="top" | ||
| | | | ||
const struct somestruct [] | ''const struct somestruct [] '' | ||
| | | | ||
Array of constant data structure values | Array of constant data structure values | ||
| | | | ||
When you don't want code to be able to change any data structure values in the array | When you don't want code to be able to change any data structure values in the array | ||
|- | |- valign="top" | ||
| | | | ||
struct somestruct * | ''struct somestruct *'' | ||
| | | | ||
Pointer to a data structure value | Pointer to a data structure value | ||
Line 169: | Line 172: | ||
When you want to allocate a single data structure or an array of data structures using ''new''<br> | When you want to allocate a single data structure or an array of data structures using ''new''<br> | ||
When you want to pass a single data structure or an array of data structures to a function, or to return same from a function | When you want to pass a single data structure or an array of data structures to a function, or to return same from a function | ||
|- | |- valign="top" | ||
| | | | ||
const struct somestruct * | ''const struct somestruct *'' | ||
| | | | ||
Constant pointer to a data structure value | Constant pointer to a data structure value | ||
| | | | ||
Same as above, but when you don't want the function that gets called, or the code that called the function, to change any values in any data structure | Same as above, but when you don't want the function that gets called, or the code that called the function, to change any values in any data structure | ||
|- | |- valign="top" | ||
| | | | ||
struct somestruct * & | ''struct somestruct * &'' | ||
| | | | ||
Reference to a pointer to a data structure value | Reference to a pointer to a data structure value | ||
Line 188: | Line 191: | ||
If ''&'' is used in a declaration, then it is defining a '''reference to a value''': | If ''&'' is used in a declaration, then it is defining a '''reference to a value''': | ||
<source lang="cpp"> | |||
data-type & variable-name; // Reference to a value | |||
const data-type & variable-name; // Constant reference to a value | |||
</source> | |||
If & is used in a statement, then it is behaving as the '''address-of operator''', and creating a '''pointer to a value''': | If & is used in a statement, then it is behaving as the '''address-of operator''', and creating a '''pointer to a value''': | ||
Line 212: | Line 217: | ||
If we have a data structure: | If we have a data structure: | ||
<source lang="cpp"> | |||
struct somestruct | |||
{ | |||
char * string_member; | |||
int int_member; | |||
double floating_point_member; | |||
}; | |||
</source> | |||
We can declare an '''instance''' of that structure and '''initialize''' (assign values to each member of) the instance: | We can declare an '''instance''' of that structure and '''initialize''' (assign values to each member of) the instance: | ||
<source lang="cpp"> | |||
somestruct aStruct = { "somestring", 1, 3.14159265 }; | |||
</source> | |||
We can declare a '''pointer to a somestruct''' by declaring a '''pointer variable''' and taking the '''address of''' the instance: | We can declare a '''pointer to a somestruct''' by declaring a '''pointer variable''' and taking the '''address of''' the instance: | ||
<source lang="cpp"> | |||
somestruct * pStruct = &aStruct; | |||
</source> | |||
or we can declare a '''reference to a somestruct''' by creating a '''reference variable''': | or we can declare a '''reference to a somestruct''' by creating a '''reference variable''': | ||
<source lang="cpp"> | |||
somestruct & refStruct = aStruct; | |||
</source> | |||
or a '''constant reference''': | or a '''constant reference''': | ||
<source lang="cpp"> | |||
const somestruct & crefStruct = aStruct; | |||
</source> | |||
or a '''pointer to a constant somestruct''': | or a '''pointer to a constant somestruct''': | ||
<source lang="cpp"> | |||
const somestruct * cpStruct = &aStruct; | |||
</source> | |||
or a '''constant pointer to somestruct''': | or a '''constant pointer to somestruct''': | ||
<source lang="cpp"> | |||
somestruct * const pcStruct = &aStruct; | |||
</source> | |||
Using any '''pointer to somestruct''' (constant or otherwise), to access a member, use the indirect '''reference operator''', ->: | Using any '''pointer to somestruct''' (constant or otherwise), to access a member, use the indirect '''reference operator''', ->: | ||
<source lang="cpp"> | |||
// read a data member | |||
cout << pcStruct->string_member << endl; | |||
cout << cpStruct->int_member << endl; | |||
cout << pStruct->floating_point_member << endl; | |||
// write to a data member | |||
pcStruct->string_member = "Test"; | |||
// ^^^^^ OK, only the pointer is constant, not what it points to. | |||
// cpStruct->int_member++; | |||
// ^^^^^^^ Not allowed! cpStruct is a pointer to a constant somestruct. | |||
pStruct->double_member = 2.0 * PI * radius; | |||
// ^^^^^ OK, neither the pointer nor what it points to is constant. | |||
</source> | |||
We can even declare a '''reference to a somestruct through a pointer''': | We can even declare a '''reference to a somestruct through a pointer''': | ||
<source lang="cpp"> | |||
somestruct & refStruct2 = *pcStruct; // dereference the pointers | |||
somestruct & refStruct3 = *pStruct; | |||
const somestruct & refStruct4 = *cpStruct; // must be constant reference | |||
</source> |
Latest revision as of 04:11, 20 October 2008
Simple C++ Data Types
C++ code | What it means | When to use it |
---|---|---|
char |
Single (scalar) value (character, integer, floating point number) |
To store data |
const char |
Constant value |
To declare a constant -- a value that cannot be changed |
char [] |
Array (vector) of values |
To store the same type of data together |
const char [] |
Constant array of values |
To declare an array of constant values (no element in the array can be changed) |
char * |
Pointer to a single value or an array of values |
To store a string (char *) |
const char * |
Pointer to a constant value |
When you don't want the value at the pointer to be changed, but you can still change the pointer |
char & |
Reference to a value |
To pass a value to a function which will be set by the function (without using a pointer) |
char * & |
Reference to a pointer |
To pass a pointer to a function where the function will set the pointer (either by calling new or by assigning to the pointer |
char * const |
Constant pointer |
When you want to point to a specific value. Allows to you to change the data that is pointed to, but you can't change the pointer |
C++Structures
C++ code | What it means | When to use it |
---|---|---|
struct somestruct |
Data structure (user-defined) |
To store different types of data together |
struct somestruct |
Data structure value |
To store values in a data structure |
const struct somestruct |
Constant data structure value |
When you don't want of the member data values to be changed |
struct somestruct & |
Reference to a data structure value |
To pass a data structure to a function where the function will change some data members of that structure |
const struct somestruct & |
Constant reference to a data structure value |
To pass a data structure to a function where the function will not or cannot change any data members of that structure |
struct somestruct [] |
Array of data structure values |
Used to group several identical data structures (with different values) together |
const struct somestruct [] |
Array of constant data structure values |
When you don't want code to be able to change any data structure values in the array |
struct somestruct * |
Pointer to a data structure value |
When you want to allocate a single data structure or an array of data structures using new |
const struct somestruct * |
Constant pointer to a data structure value |
Same as above, but when you don't want the function that gets called, or the code that called the function, to change any values in any data structure |
struct somestruct * & |
Reference to a pointer to a data structure value |
When the calling function will be allocating the pointer using new or assigning to the pointer |
Using The References and Pointers: * & . and ->
If & is used in a declaration, then it is defining a reference to a value: <source lang="cpp">
data-type & variable-name; // Reference to a value const data-type & variable-name; // Constant reference to a value
</source>
If & is used in a statement, then it is behaving as the address-of operator, and creating a pointer to a value:
- &variable-name
If * is used between two variables or constants of type int or double, then it is behaving as the multiplication operator:
- variable-name1 * variable-name2
- constant * variable-name
- variable-name * constant
- constant1 * constant2
If * is used in a declaration, then it is defining a pointer:
- data-type * variable-name Pointer to a value
- const data-type * variable-name Pointer to a constant value
- data-type * const variable-name Constant pointer to a value
- const data-type * const variable-name Constant pointer to a constant value
If * is used in a statement, then it is behaving as the dereferencing operator, and creating a value from a pointer:
- *pointer-variable-name
Using Pointers and References To Structures
If we have a data structure:
<source lang="cpp"> struct somestruct {
char * string_member; int int_member; double floating_point_member;
}; </source>
We can declare an instance of that structure and initialize (assign values to each member of) the instance: <source lang="cpp"> somestruct aStruct = { "somestring", 1, 3.14159265 }; </source>
We can declare a pointer to a somestruct by declaring a pointer variable and taking the address of the instance: <source lang="cpp"> somestruct * pStruct = &aStruct; </source>
or we can declare a reference to a somestruct by creating a reference variable: <source lang="cpp"> somestruct & refStruct = aStruct; </source>
or a constant reference: <source lang="cpp"> const somestruct & crefStruct = aStruct; </source>
or a pointer to a constant somestruct: <source lang="cpp"> const somestruct * cpStruct = &aStruct; </source>
or a constant pointer to somestruct: <source lang="cpp"> somestruct * const pcStruct = &aStruct; </source>
Using any pointer to somestruct (constant or otherwise), to access a member, use the indirect reference operator, ->: <source lang="cpp"> // read a data member cout << pcStruct->string_member << endl; cout << cpStruct->int_member << endl; cout << pStruct->floating_point_member << endl;
// write to a data member pcStruct->string_member = "Test"; // ^^^^^ OK, only the pointer is constant, not what it points to.
// cpStruct->int_member++; // ^^^^^^^ Not allowed! cpStruct is a pointer to a constant somestruct.
pStruct->double_member = 2.0 * PI * radius; // ^^^^^ OK, neither the pointer nor what it points to is constant. </source>
We can even declare a reference to a somestruct through a pointer:
<source lang="cpp"> somestruct & refStruct2 = *pcStruct; // dereference the pointers somestruct & refStruct3 = *pStruct; const somestruct & refStruct4 = *cpStruct; // must be constant reference </source>