C++. Classes. Types of functions of class members. Simple member functions. The static member functions. Examples of using




C++. Classes. Types of functions of class members. Simple member functions. The static member functions. Examples of using

This topic is based on the use of member functions of the class, oriented to the Visual C++ compiler. In the implementations of compilers of different developers, there may be significant differences, especially with regard to static member functions.


Contents


Search other websites:

1. What kinds of member functions can be declared in classes?

In classes, you can declare the following kinds of functions:

  • the ordinary member functions. These are functions whose declaration does not use additional keywords (const, volatile, static);
  • static functions. These are functions declared with the ‘static’ keyword;
  • constant functions. These are functions that are declared with the keyword const;
  • member functions that return a volatile object. These are functions declared with the keyword ‘volatile’;
  • member functions with a constant ‘this’ pointer;
  • member functions with a volatile pointer ‘this’;
  • inline member functions.





2. Example of declaring a class that contains ordinary member functions

In this example, a CLine class is declared that describes a line on the coordinate plane. The line consists of the coordinates of the points (x1; y1), (x2; y2). The class contains simple member functions declared in the public section. Using these member functions, the internal class variables are read and modified.

// class that describes a line in the coordinate plane
class CLine
{
    int x1, y1;
    int x2, y2;

    public:
    CLine(void);
    ~CLine(void);

    // class methods
    void SetLine(int nx1, int ny1, int nx2, int ny2);
    int GetX1(void) { return x1; }
    int GetY1(void) { return y1; }
    int GetX2(void) { return x2; }
    int GetY2(void) { return y2; }
};

// Implementation of class methods
// the default constructor
CLine::CLine(void)
{
    x1 = y1 = 0;
    x2 = y2 = 1;
}

// destructor
CLine::~CLine(void)
{
}

// the implementation of a simple class member function
void CLine::SetLine(int nx1, int ny1, int nx2, int ny2)
{
    x1 = nx1;
    y1 = ny1;
    x2 = nx2;
    y2 = ny2;
}

Using a class in another program code

// the declaration of CLine object
CLine CL;
int t;

// call the class member functions
CL.SetLine(5, 6, 10, 20);
t = CL.GetX2(); // t = 10
t = CL.GetY1(); // t = 6

3. Example of declaring a class containing static member functions

In this example, a class called CCircle is declared that describes a circle on the coordinate plane. The class implements the following internal data members and methods:

  • internal variables (members) of class x, y, r (the coordinates of the center and the radius of the circle);
  • constructor of class CCircle(), which defines a circle with radius r = 1 and center at (0; 0);
  • a static member function of the SetData1() class that sets the value of the global variables xx, yy, rr declared outside the class;
  • the ordinary (non-static) member function of the SetData2() class, which changes the value of the internal variables x, y, r of the class;
  • ordinary (non-static) methods of the class, realizing the reading of the values of the internal variables of the class x, y, r and global variables xx, yy, rr.
// class implementing a circle on the coordinate plane
class CCircle
{
    // internal variables of class
    int x, y; // coordinates of the center of a circle
    int r; // radius of a circle

    public:
    // constructor
    CCircle(void);

    // a static method - only for global variables
    static void SetData1(int nx, int ny, int nr);

    // ordinary (non static) class methods
    // set new values of internal class variables
    void SetData2(int nx, int ny, int nr);

    // take the value of internal class variables
    int GetX(void) { return x; }
    int GetY(void) { return y; }
    int GetR(void) { return r; }

    // take the value of global class variables
    int GetXX(void);
    int GetYY(void);
    int GetRR(void);
};

// global variables
int xx, yy, rr; // the static member function of the class can change these variables

// class constructor
CCircle::CCircle(void)
{
    // filling the values of internal (local) variables
    x = 0; y = 0; r = 1;
}

// static member-function of class
void CCircle::SetData1(int nx, int ny, int nr)
{
    // assigning values to global variables declared outside the class
    xx = nx;
    yy = ny;
    rr = nr;

    // Working with internal class variables in a static function is forbidden.
    // x = nx; // Forbidden! Error:"... illegal reference to non-static member 'CCircle::х'
    // y = ny; // is also forbidden
}

// set new values of ordinary (non-static) methods of the class
void CCircle::SetData2(int nx, int ny, int nr)
{
    x = nx;
    y = ny;
    r = nr;
}

// read the values of global class variables
int CCircle::GetXX(void)
{
    return xx;
}

int CCircle::GetYY(void)
{
    return yy;
}

int CCircle::GetRR(void)
{
    return rr;
}

Change the values of internal class variables in a static member function is forbidden. If you try to change the values of internal variables of class x, y in the SetData1() function

void CCircle::SetData1(int nx, int ny, int nr)
{
    ...
    x = nx;
    y = ny;
    ...
}

the compiler will generate an error

... illegal reference to non-static member 'CCircle::x'

If you want to change the values of internal class variables in a static function, then it is needed to pass to this function the pointer of this class.

The following code demonstrates the use of a static member function for the Circle class

// the use of class CCircle
CCircle C1;
int tx, ty, tr; // additional variables

// Calling a static member function
C1.SetData1(2, 3, 4); // only the global variables xx, yy, rr are changed

// read the value of internal class variables
tx = C1.Get(); // tx = 0 - internal variables x, y, r are initialized by the class constructor
tr = C1.Get(); // tr = 1

// read the value of global variables declared outside the class
tx = C1.GetXX(); // tx = 2
ty = C1.GetYY(); // ty = 3

// calling an ordinary (non-static) member function of the class
C1.SetData2(2, 3, 4); // only the internal variables x, y, r are changed
tx = C1.GetX(); // tx = 2
tr = C1.GetR(); // tr = 4

// Another way to call a static member function
CCircle::SetData1(7, 8, 9);
tx = C1.GetXX(); // tx = 7
ty = C1.GetYY(); // ty = 8
4. What is the difference between static member functions and ordinary member functions

The main differences between static member functions and ordinary ones:

  • You can call a static member function without reference to the class object, for example CMyClass::MyStaticFuntion(…). In this case, the scope extension operator ‘::’ is used;
  • static member functions do not receive ‘this’ class’s pointer. This means that they can not access the internal members of the class data. The exception is static class data members, which are declared with the ‘static’ keyword;
  • in C++, the static member function of the class is global. Therefore, it is advisable to use it to change the values of global variables that are declared outside the class but have an using in the class.

5. In which cases is it appropriate to use static member functions?

It is advisable to use static member functions of a class in cases where the class needs to use global variables that are common to different objects (instances) of this class.


Related topics