Classes and arrays. Arrays of pointers to the class members methods. Examples

Classes and arrays. Arrays of pointers to the class members methods. Examples


Contents


1. Declaring an array of pointers to methods that are members of the class using the typedef tool. General form

In C ++, you can declare a pointer to a method of some class. So, you can declare an array of pointers to the methods of a certain class. It is clear that the methods signature can be the same.

To declare an array of pointers to methods of class A, you need to declare a type that is a pointer to a method of class A. To do this, you need to use the typedef tool using the sample, as shown below

typedef type (A:: * PType)(arguments_list);

where

  • type – a type, which the methods of the class return;
  • argument_list – list of arguments of the the class methods (must be the same for all methods);
  • A – the name of the class in which you declare methods that you want to combine into an array;
  • PType – the name of the new type that defines a pointer to a method from class A.

If the PType type is defined, then the array of pointers can be declared in the following form

PType ArrayName[size];

where

  • PType – a known type that is a pointer to a method of class A;
  • ArrayName – the name of the array of pointers to methods of class A;
  • size – the number of elements in the array of ArrayName pointers.

To set the value of an array to some method of class A, you can use the following general form

ArrayName[index] = &A::SomeMethod;

where

  • ArrayName – the name of array of pointers;
  • index – position (index) in the array of pointers;
  • A – a class whose methods are pointed to by an array of pointers;
  • SomeMethod – some method of class A.

 

2. Example of declaring and using an array of pointers to methods that are members of a class

An example, which demonstrates the use of array of pointers to the class methods, is given. In the example are declared:

  • a class named X;
  • a global object of class named obj;
  • a data type FP, which desrcibes a pointer to the class method X;
  • an array of pointers A, which is initialized by the addresses of methods of class X;
  • the global function SetMember().

In the class are declared:

  • internal class variables value, total, count;
  • class constructor by default;
  • access methods SetV(), SetT(), SetC(). By the values of addresses (entry points) of these methods will initialize the global array of pointers A;
  • methods for reading internal variables of class GetV(), GetT(), GetC().

Listing of a class with the function _tmain() for an application of type Win32 Console Application looks like this:

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

// declaring a class X
class X
{
    int value;
    int total;
    int count;

    public:
    X() { value = total = count = 0; }
    void SetV(int nv) { value = nv; }
    void SetT(int nt) { total = nt; }
    void SetC(int nc) { count = nc; }
    int GetV(void) { return value; }
    int GetT(void) { return total; }
    int GetC(void) { return count; }
};

X objX;

typedef void (X::*FP)(int); // new data type FP
FP A[] =
{
    &X::SetV, // A[0] <=> &X::SetV
    &X::SetT,
    &X::SetC,
    NULL
};

// function that sets the pointer
void SetMember(void (X::*fPtr)(int), int t)
{
    (objX.*fPtr)(t); // call the method from object using the pointer
}

// entry point into the program
int _tmain(int argc, _TCHAR* argv[])
{
    // arrays of pointers to member functions of the class
    SetMember(&X::SetV, 8); // objX.value = 8

    int t;
    t = objX.GetV(); // t = 8

    SetMember(A[0], 10);
    t = objX.GetV(); // t = 10

    SetMember(A[1], 15);
    t = objX.GetT(); // t = 15

    // the use of a simple pointer to a function
    void (X::*pF)(int);

    pF = &X::SetC; // pF points to the SetC() method of some class
    SetMember(pF, 13); // calling the SetMember (), in which the pointer is converted
    t = objX.GetC(); // t = 13

    // setting the last element of array A to the method X::SetT
    A[3] = &X::SetT; // demonstration of use the pointer to the class method

    return 0;
}

 

3. How can I declare an array of pointers to class methods without using the typedef tool? General form

An array of pointers to class methods can be declared without using the typedef keyword. The general form of such declaring:

type (A:: * ArrayName[size])(arguments_list);

where

  • type – type returned by the methods of the class to which the array of pointers is declared;
  • ArrayName – name of the array of pointers;
  • size – array size;
  • arguments_list – list of arguments that get the methods of the class to which the array of pointers is declared.

For example. To the methods of class X, described in paragraph 2 you can declare the array B of pointers as follows:

// array of pointers to the methods of class X without using typedef
void (X::*B[5])(int);

If you want to initialize array B, you can write this:

// array of pointers to the methods of class X without using typedef
void (X::*B[5])(int) = 
{
    &X::SetC,
    &X::SetV,
    NULL,
    NULL,
    NULL
};

Assigning values to the elements of array B may be as follows:

// assignment of values
B[2] = &X::SetV;
B[3] = &X::SetC;
B[4] = &X::SetT;

 


Related topics