C++. Constant member functions. Member functions ‘volatile’




Constant member functions. Member functions ‘volatile’


Contents


Search other websites:

1. What functions are called constant member functions of a class?

Functions that return a constant object are called constant functions. If such functions are declared in the class, then these functions are called constant member functions of the class.

To declare a function that returns a constant object, you must place the keyword ‘const’ before the function declaration

const returned_type FunName(parameters)
{
    // function's body
    // ...
}

where

  • returned_type – the type returned by the function;
  • parameters – function’s parameters;
  • FunName – the name of the function that returns a constant object (constant function).

If the function is declared in the class, then its declaration will have approximately the following form:

class CMyClass
{
    private:
    // private members and class methods
    // ...

    public:
    // public members and class methods
    // ...

    // declaration of a constant function in a class
    const returned_type FunName(parameters);
}

// FunName function implementation
const returned_type CMyClass::FunName(parameters)
{
    // body of FunName() function
    // ...
}

2. Example of declaring a class that contains constant (const) member functions

The example declares a CMyClass class containing a constant function Get() that returns a constant value of type double.

// class that contains declarations of a constant function
class CMyClass
{
    double x; // internal variable

    public:
    CMyClass(void); // constructor

    void Set(double nx);
    const double Get(void); // a constant member function of class
};

Using a constant function from another code:

// use of a constant function
CMyClass MC;

MC.Set(25.08); // setting the value of x

// using the constant function Get() of CMyClass class
double t1 = MC.Get(); // t1 = 25.08 - initialization of variable
const double t2 = MC.Get(); // t2 = 25.08 - initialization of constant

3. What is the general view of the member function of a class that returns an object of type volatile?

An object declared with the ‘volatile’ qualifier can be changed implicitly in the program – without using explicitly specified commands. The keyword ‘volatile’ informs the compiler that the value of a variable in the program can be changed implicitly (interrupt processing program, background process, etc.). Knowing such variability of the variable in the program (the keyword ‘volatile’), the compiler will generate code that will examine the value of the variable before each use in the program. Thus, the real (changed) value of the variable will be received (and not what was before the change).






For example. Let in some program P the global variable X is implemented, containing the address, which can be changed by the program of interrupt processing. The interrupt’s handler program will be called regardless of the execution of this program P and accordingly changes the value of this global variable X. If a program P does not specify the volatile keyword in front of X, then, by reading the value of X in P program can display the old value of X. If you specify the keyword ‘volatile’, the compiler will update the values of the variable X each time it is accessed, and the result will not be distorted.

Member functions that are declared with the ‘volatile’ specifier simply return a volatile object. Such an object can be assigned to a variable that is declared with the ‘volatile’ specifier. General view of a class containing a function that returns a ‘volatile’ value

class CMyClass
{
    private:
    // private members and class methods
    // ...

    public:
    // public members and class methods
    // ...

    // declaration of a constant function in a class
    volatile returned_type FunName(parameters);
}

// implementation of FunName() function
volatile returned_type CMyClass::FunName(parameters)
{
    // body of function FunName()
    // ...
}

where

  • returned_type – type that is returned by the function;
  • parameters – parameters of function;
  • FunName – the name of the function that returns a ‘volatile’ object.

4. Example of declaration of a class containing member functions ‘volatile’

In the CMyVolatileClass class, the function Get() is declared, which returns a volatile value of type int.

// class that contains a 'volatile' function
class CMyVolatileClass
{
    int d;

    public:
    CMyVolatileClass(void);

    // member functions of class
    // the ordinary member-function of class
    void Set(int nd) { d = nd; }

    // member-function that returns a volatile value
    volatile int Get(void);
};

Demonstrating the use of a class in some method or code:

// class containing a volatile function
CMyVolatileClass VC;

VC.Set(33); // calling a normal function

// calling the volatile function for a regular variable
int t1 = VC.Get(); // t1 = 33

volatile int t2;

// calling the volatile-function for volatile-variable
t2 = VC.Get(); // t2 = 33

As you can see from the above code, volatile-functions can be used in combination with normal and volatile variables.


Related topics