C++. Classes. Using the class data members. Static data members in the class. Static data members for native classes and managed classes in the CLR




C++. Classes. Part 3. Using the class data members. Static data members in the class. Static data members for native classes and managed classes in CLR


Contents


Search other websites:

1. What types of access can be applied to the data?

The data and methods of the class are members of the class. During the data definition, all known access methods can be used. Data can be declared as private, protected, public.

More details about the types of access are described in the topic:

2. Is it possible to initialize the data inside the class during them declaration?

When you declare a class, you can not initialize data. You can initialize data in class methods, class constructors, external methods, and so on. A class is not an object, and memory is not allocated for it, until an instance of the class is created. The data in the class is similar to the structure fields.

3. How do I declare a static member of data in a native-class? Example of declaration and using

A static data member in a class is declared using the static keyword. Because this is a static member of the data, you need to define it outside the class. All instances (objects) of the class have access to this static data member.

To use a static data member in a class, you need:

  • declare a static member of the data in the body of the class (in any section of the class);
  • define a static member of the data outside the class.

Example. In the native class (unmanaged-class) of CMyClass, a static data member with the name d of the integer type is declared.

// native class declaration
class CMyClass
{
    static int d; // only declaration the static member, memory is not allocated

    public:
    CMyClass(void); // constructor
    ~CMyClass(void); // destructor

    // methods for accessing a static data member
    int Get(void) { return d; }
    void Set(int nd) { d = nd; }
};

// defining a static member of data outside the class
int CMyClass::d; // memory is allocated

// explicit default constructor
CMyClass::CMyClass(void)
{
    d = 0;
}

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

As you can see from the above code, using the operator of the ‘::’ access extension, a static member of the data is defined.

Using a static d member declared in the CMyClass class from another method (program code):

// using a static member of the class
CMyClass MC1; // object (instance) of class 1
CMyClass MC2; // object (instance) of class 2

MC2.Set(25);
int t;
t = MC1.Get(); // t = 25 - Both objects have access to one memory location

4. What is true regarding the use of a static member of the class by instances of this class?

If a static member is declared in the class, then all instances (objects) of the class share this static member of the class. More precisely, this static member of the class is common to all objects of this class. Unlike automatic data (data without the word ‘static’), multiple copies of static data can not be created.






5. In which memory area is the static data member stored?

A static data member is stored (allocated) in a fixed data area. This occurs at the linking stage.

6. How many times is the memory allocated to a static member of the data?

The memory for the static data member is allocated at the linking stage only once at the time when it is determined. In the above example (step 3), memory is allocated in a row

int CMyClass::d;

7. Can I initialize a static member of a class when it is declared in a native-class?

Yes. The static member of the class data is initialized when it is defined. For example:

int CMyClass::d = 10; // initializing a static data member

8. How do I declare a static member of data in a managed class? Example of declaring and using

Unlike native-classes, in managed classes the declaration of a static member of the data is performed as an automatic member (without additional definition of the data member). You must specify the ‘static’ keyword.

In managed classes, the instances of the class have access to one to one fixed memory location (just as in native-classes).

Example. Let it be given the class CMyRefClass, declared as managed (with the keyword ref). Такие классы используются в CLR.

// declaration of a managed-class
ref class CMyRefClass
{
    private:
    static int rd; // declaring a static member of data in the class - only once

    public:
    CMyRefClass(void); // default constructor

    // class methods
    int Get(void) { return rd; }
    void Set(int nrd) { rd = nrd; }
};

// the definition of a static member - is not necessary!
//int CMyRefClass::rd;

// constructor
CMyRefClass::CMyRefClass(void)
{
    rd = 0;
}

As you can see from the above code, for a managed class, you do not need to separately define a static member. Using this class from another method or program code:

// static data members in managed classes
CMyRefClass RC1;
CMyRefClass RC2;
CMyRefClass ^RC3 = gcnew CMyRefClass(); // managed pointer to class

RC2.Set(5);

int t;
t = RC1.Get(); // t = 5

RC3->Set(15);
t = RC2.Get(); // t = 15

RC1.Set(-20);
t = RC3->Get(); // t = -20

As you can see from the above code, static data members in all classes are common to instances of these classes, even if the memory is allocated to the class instance using the gcnew operator.

9. What are the ways to access public static members of the data?

If a static data member is declared in the public section, you can access it in one of three ways:

  • with the symbol ‘ . ‘ (point) if an object (instance) of the class is declared;
  • using a sequence of ‘->’ characters if a pointer to a class object is declared;
  • using the class name and the ‘::’ character (scope extension). This method also works for static data members declared in ‘private’ or ‘protected’ sections.

10. Example of using a public static member for the native-class

Let the native class CMyClass in which the public static member is declared is given. The following code demonstrates how to access a public static member of a class.

// native-class
class CMyPublicClass
{
    public:
    CMyPublicClass(void);
    ~CMyPublicClass(void);

    static int d; // public static member of the class

    // class methods
    int Get() { return d; }
    void Set(int nd) { d = nd; }
};

// static member definition
int CMyPublicClass::d;

// constructor
CMyPublicClass::CMyPublicClass(void)
{
    d = 0;
}

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

Demonstration of access to a static member of a class from another program code

// demonstrating of access to a public static member of the class
CMyPublicClass PC1;
CMyPublicClass * PC2 = new CMyPublicClass(); // class pointer
int t;

// access with the symbol '.'
PC1.d = 30;
t = PC1.Get(); // t = 30

// access using the pointer ->
PC2->d = 50;
t = PC2->Get(); // t = 50
t = PC1.Get(); // t = 50

// Access using the scope extension operator ::
CMyPublicClass::d = -33;
t = PC2->d; // t = -33
t = PC1.d; // t = -33

11. An example of using a public static member for a managed-class

For managed-class the access to the static member of the class is the same as for the native class. Let the CMyPublicClass managed-class declarations be given.

// managed-class (with identifier ref)
ref class CMyPublicClass
{
    public:
    CMyPublicClass(void);
    ~CMyPublicClass(void);

    static int d; // public static member of the class

    // class methods
    int Get() { return d; }
    void Set(int nd) { d = nd; }
};

// constructor
CMyPublicClass::CMyPublicClass(void)
{
    d = 0;
}

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

Demonstration of various types of access

// demonstrating of access to a public static member of the class
CMyPublicClass PC1;
CMyPublicClass ^PC2 = gcnew CMyPublicClass(); // managed-ponter to class
int t;

// access with the symbol '.'
PC1.d = 30;
t = PC1.Get(); // t = 30

// access using the pointer ->
PC2->d = 50;
t = PC2->Get(); // t = 50
t = PC1.Get(); // t = 50

// Access using the scope extension operator ::
CMyPublicClass::d = -33;
t = PC2->d; // t = -33
t = PC1.d; // t = -33

12. What are static data members for? The advantages of using static data members

Static members of this class can be used to bind class objects with each other. They are the common memory area that different objects of a class can use, which are declared in different methods. The memory for the static member of the class is always allocated, even if no object is declared in some method.

For example, you can declare a class that contains a static data member that counts the number of active objects in the class. In this case, methods that increase (decrease) the value of this static member are implemented. These methods can be called from different parts of the program code (methods of other classes, etc.). However, the memory that is allocated to this static data member will be common to all of these methods.

13. What are the ways to access the hidden static members of the class (declared in the private section)?

If the static member of the class is declared in the private section, you can access it in one of three ways:

  • using the scope extension operator ‘::’;
  • using the member function of the class;
  • using a class that is declared friendly to this class.

If to access the hidden static data member you can use the ‘ . ‘ operator or ‘->’ pointer, the compiler will generate an error.


Related topics