References, Pointers and Constants: Difference between revisions
No edit summary |
|||
Line 101: | Line 101: | ||
|} | |} | ||
==C++Structures== | |||
{|width="100%" border="1" cellpadding="2" cellspacing="0" | {|width="100%" border="1" cellpadding="2" cellspacing="0" | ||
|- | |- | ||
Line 108: | Line 108: | ||
!style="color:#ffb900"|When To Use It | !style="color:#ffb900"|When To Use It | ||
|- | |- | ||
| | | | ||
| | struct somestruct<br> | ||
| | {<br> | ||
. . .<br> | |||
}; | |||
| | |||
Data structure (user-defined) | |||
| | |||
To store different types of data together | |||
|- | |||
| | |||
struct somestruct<br> | |||
(struct is optional) | |||
| | |||
Data structure value | |||
| | |||
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!) | |||
|- | |||
| | |||
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'''<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 | |||
|- | |||
| | |||
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 | |||
|} | |} |
Revision as of 07:39, 14 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 |