Classes and arrays. A pointer to a static member of the class data. Arrays of static data members. Arrays of pointers to static data members that are declared in classes. Examples

Classes and arrays. A pointer to a static member of the class data. Arrays of static data members. Arrays of pointers to static data members that are declared in classes. Examples


Contents


1. How do I declare a pointer to a static member of a class data? Example

To declare a pointer to a static member of the class data, you simply need to declare a pointer to the data type that has a static data member. Then, this pointer needs to be assigned the static member address of the object data of this class.

The sequence of steps is as follows:

  • declare a class with a static member;
  • declare the static member of the data class outside the class and initialize it if necessary;
  • in some method or outside all methods (global pointer), declare a pointer to a type that has a static member of the class data;
  • declare a class object with a static member of the class;
  • assign the address of the static member of the data of the class object to the pointer.

Example. The example declares a CPi class with a static member of the Pi data. Also, several static members are declared that are not members of the class.

The function _tmain() demonstrates the use of pointers to declared static data members.

#include "stdafx.h"
#include <iostream>
using namespace std;

// declaring a class that includes a static data member
class CPi
{
    public:
    static double Pi; // static data member
};

// declaration of a static variable outside the class with simultaneous initialization
static double Exp = 2.7182818284; // exponent

// initialization of static member of class CPi
double CPi::Pi = 3.141592653589793;

// uninitialized static variables whose values are 0 (default)
static double ZeroD;
static int ZeroI;
static char ZeroC;

int * pZ; // external pointer to int

int _tmain(int argc, _TCHAR* argv[])
{
    double d;
    CPi obj; // class object
    double * pPi; // pointer to double
    double * pExp; // pointer to double

    // the pointer points to the static data member of the object obj of class CPi
    pPi = &obj.Pi;
    pExp = &::Exp; // points to a static member of the data Exp - another type of access through ::

    // test
    d = obj.Pi; // d = 3.14159
    d = Exp; // d = 2.71828
    d = ::Exp; // d = 2.71828 - so can you too
    d = *pPi; // d = 3.14159

    // external pointer to int
    pZ = &ZeroI;
    d = *pZ; // d = 0

    *pZ = 5;
    d = ZeroI; // d = 5

    return 0;
}

 

2. An example of using an array whose elements are static members of the class data

There are 3 classes with the names CCounter1, CCounter2, CCounter3. Each class implements one static data member, respectively, with the name counter1, counter2, counter3.

Because, in classes there are static data members, they can be initialized accordingly. Also, the example declares a global array named arCount1[], which is initialized by static members of the CCounter1, CCounter2, CCounter3 classes.

Listing of class declarations and initialization of static members is as follows:

// The 3 classes with a static variables are declared
// class containing the static variable 'count'
class CCount1
{
    public:
    static int count1; // static data member, the keyword 'static'
};

class CCount2
{
    public:
    static int count2; // static data member, the keyword 'static'
};

class CCount3
{
    public:
    static int count3; // статический член данных, ключевое слово static
};

// declaration, initialization of static members of the classes
// here the keyword static is not needed
int CCount1::count1; // by default the value of static member is 0
int CCount2::count2 = 5;
int CCount3::count3 = 100;

// An array declaration that is initialized by static data members
int arCount1[] =
{
    CCount1::count1,
    CCount2::count2,
    CCount3::count3
};

The use of the global array arCount1[] and the local array arCount2[] declared directly in the _tmain() function for console applications is demonstrated below:

int _tmain(int argc, _TCHAR* argv[])
{
    // using of arCount1 array
    int d;

    d = arCount1[0]; // d = 0
    d = arCount1[1]; // d = 5
    d = arCount1[2]; // d = 100

    // change the value of array arCount1[2] so,
    // that it contains the value of the static member of class CCount1
    arCount1[2] = CCount1::count1;
    d = arCount1[2]; // d = 5

    // declaring the local array arCount2
    int arCount2[5];

    // filling of array arCount2 by values of static members
    arCount2[0] = CCount1::count1;
    arCount2[1] = CCount2::count2;
    d = arCount2[1]; // d = 5

    return 0;
}

 

3. An example of using an array whose elements have pointers to static data members of the class

In this example, according to the example of the previous example (item 2), 3 classes with the names CCounter1, CCounter2, CCounter3 are declared. In these classes, one static data member is declared with the names counter1, counter2, counter3, which are individually initialized with initial values.

The example demonstrates the declaration and use of arPCount1 and arPCount2 arrays to static data members that are members of data of some class.

The listing of class declarations, static data members, and an array of pointers is as follows:

// the 3 classes with static variables are declared
// class containing the static variable 'count'
class CCount1
{
    public:
    static int count1; // static data member
};

class CCount2
{
    public:
    static int count2; // static data member
};

class CCount3
{
    public:
    static int count3; // static data member
};

// declaration, initialization of static members of classes
int CCount1::count1; // by default the value of static data member is 0
int CCount2::count2 = 5;
int CCount3::count3 = 100;

// declaration of a global array of pointers to static data members
int * arPCount1[] =
{
    &CCount1::count1,
    &CCount2::count2,
    &CCount3::count3
};

Demonstration of using a pointer array in the function _tmain()

int _tmain(int argc, _TCHAR* argv[])
{
    // using of array of pointers arPCount1
    d = *arPCount1[0]; // d = 0
    d = *arPCount1[1]; // d = 5

    // change the value of array arPCount1 [2] so,
    // so that it points to a static CCount2 member
    arPCount1[2] = &CCount2::count2;
    d = *arPCount1[2]; // d = 100

    // declaration of local array arPCount2
    int * arPCount2[5];

    // filling of arPCount2 array with some values and using them
    arPCount2[0] = &CCount1::count1;
    arPCount2[1] = &CCount3::count3;
    d = *arPCount2[1]; // d = 100

    return 0;
}

 

4. What is the value of a static member if it is not initialized?

If the static member is not initialized, the default value is set:

  • for numeric types (int, float, double …), the value of the uninitialized static member is 0;
  • for char, the value of the uninitialized static member is equal to the character with the code 0;
  • for the bool type, the value is false, which also corresponds to the value 0.


Related topics