References and Pointers

References


Example

// Declare and initialize an integer to 10.
int iInteger = 10;

// Declare and set a reference to the integer.
int& irIntRef = iInteger;

// Setting the reference, sets the value of the original int.
irIntRef = 20;

Pointers


Example 1

// Declare and initialize a pointer to null
CPoint* qpPointPtr = 0;

Example 2

// Declare and initialize an integer to 10
int iInteger = 10;

// Declare and set a pointer to the integer's address
int* ipIntPtr = &iInteger;

// Setting the dereferenced pointer, sets the value of the original int
(*ipIntPtr) = 20;

// Set the pointer to point to another location
ipIntPtr = ipAnotherIntPtr;

Example 3

// Dynamically allocate an int
int* ipIntPtr = new int;

// Dereference the pointer to set the int's value
(*ipIntPtr) = 5;

// Deallocate the int
delete ipIntPtr;

Example 4

// Dynamically allocate an object of type CPoint
CPoint* qpPointPtr = new CPoint(37.2, 83.4);

// Access a member of the allocated object
qpPointPtr->SetX(65.3);

// Deallocate the CPoint
delete qpPointPtr;

Example 5

// Dynamically allocate a 10 element array
int * ipIntArray = new int[10];

// Access an element of the array
ipIntArray[2] = 4;

// Deallocate Array
delete [] ipIntArray;

Example 6

// Dynamically allocate a 5x4 2d Array
int** ippIntArray2D = new int*[5];
for (int iIndex = 0; iIndex < 5; ++ iIndex) {
    ippIntArray2D[iIndex] = new int[4];
}

// Access an element of the array
ippIntArray2D[2][1] = 10;

// Deallocate the 2d Array
for (int iIndex = 0; iIndex < 5; ++ iIndex) {
    delete [] ippIntArray2D[iIndex];
}
delete [] ippIntArray2D;

Auto Pointers


Example 1

// Allocating memory for an auto_ptr of type int
auto_ptr<int> ipIntPtr = new int;

// We can dereference just like a pointer
(*ipIntPtr) = 9;

// The memory pointed to by 'ipIntPtr' is automatically
// deallocated when 'ipIntPtr' falls out of scope

Example 2

// Allocating memory for an auto_ptr object of type CPoint
auto_ptr<CPoint> qpPointPtr = new CPoint(37.2, 83.4);

// We can members with it just like a pointer
double dCopyOfX = qpPointPtr->GetX();

// The memory pointed to by 'qpPointPtr' is automatically
// deallocated when 'qpPointPtr' falls out of scope

Function Pointers


Example 1

// Declaring a pointer for a function taking and returning a double
double (*pfnFunctionPtr)(double);

// A defined a function with the same signature
double Square(double dX) {
    return dX*dX;
}

// Pointing to the Square() function (method 1)
pfnFunctionPtr = □

// Assigning the pointer without the "address of" (method 2)
pfnFunctionPtr = Square;

// Calling the function through the pointer (method 1)
double dOneHundred = pfnFunctionPtr(10);

// Calling the function through the pointer with dereferencing (method 2)
double dOneHundred = (*pfnFunctionPtr)(10);

Example 2

// Use a typedef to create a function pointer type
typedef void (*TFunctionPtr)(int);

// Declaring a function pointer of that type
TFunctionPtr pfnFunctionPtr;

// A defined a function with the same signature
void Print(int iInteger) {
    std::cout << iInteger;
}

// Assigning the function pointer to Print()
pfnFunctionPtr = Print;

// Calling the Print() function through the pointer
pfnFunctionPtr(25);

Example 3

// Use a typedef to create a function pointer type
typedef char (*TFunctionPtr)(char, int);

// Create and initialize an array of three function pointers
TFunctionPtr qaFnPtrArray[] = { Fn1, Fn2, Fn3 };

Data Member Pointers


Example

// A defined class
class CPoint {
public:
    double mdX;
    double mdY;
};

// Declare an instance of the class.
CPoint qMyPoint;

// Declare and initialize a data member pointer to mdX
double CPoint::*mpdDataPtr = &CPoint::mdX;

// Set the mdX for qMyPoint via the pointer
qMyPoint.*mpdDataPtr = 105.34;

// Set a pointer to the address of the object
CPoint* qpMyPtr = &qMyPoint;

// Set mdX to a new value using the new pointer
qpMyPtr->*mpdDataPtr = 73.2;

Member Function Pointers


Example

// A defined class
class CPoint {
public:
    CPoint(double dX, double dY);
    double GetX();
    double GetY();
    double Distance(const CPoint&);
    double mdX;
    double mdY;
};

// Declare and initialize an instance of the class.
CPoint qMyPoint1(6.3, 2.1);
CPoint qMyPoint2(4.7, 6.8);

// Declare and initialize two member function pointers
double (CPoint::*mpfnFnPtr1)() = &CPoint::GetX;
double (CPoint::*mpfnFnPtr2)(const CPoint&) = &CPoint::Distance;

// Call GetX() on the first point through the function pointer
double dX1 = (qMyPoint1.*mpfnFnPtr1)();

// Call Distance() on the first point, passing in the second
double dDist = (qMyPoint1.*mpfnFnPtr2)(qMyPoint2);

// Declare and initialize a pointer to the second object
CPoint qpMyPtr = &qMyPoint2;

// Call GetX() on the second point through the function pointer
double dX2 = (qpMyPtr->*mpfnFnPtr1)();

// Call Distance() on the second point, passing in the first
dDist = (qpMyPtr->*mpfnFnPtr2)(qMyPoint1);