Method of using timer in non-window classes (1)

zhaozj2021-02-16  52

Method for using timer in non-window classes

Author: Liu Hui

(Net into Technology Jiangsu Kunshan Email: Jemmyliu@163.com Completion Time: March 12, 2003)

Summary This article introduces how to use timers in the Visual C window and non-window classes through some simple examples. The focus describes how to use a static member function and a static data member in a non-window class using a timer, while introducing the knowledge related to the timer, such as a callback function, a static member in the C class, and mapping in the template class. Class, etc.

Keyword C class timer static function static member function static data member callback function mapping class

Abstract:. This page introduce how to use timer in window class and none window class of Visual C by some simple samples Use timer in none window class with static member variable and static member function is the important point At the same time, it. Also Tell About of Some Knowledge Such As About Timer, Callback Function, Static Member of C Class and Map Class CMAP of Template Class.

Keywords: C Class Timer Static Callback CMAP

1 Introduction

The role of timer in Windows program is not negligible, and it is also possible. Set a time interval to refresh a clock every 0.5 second or 1 second, so you can complete a simple electronic clock program. The usage of the timer in different programming tools is also different. Visual C also provides us with ways to implement this function, and the method is not only one. In the window class, it is very simple to use the timer. After setting the timer with setTimer (), and after the ONTIMER message map is added to the Class Wizard, you can add code implementation in the mapping function ontimer (), time Complete your task and also support any multiple timers, which may be used. However, in a non-window class, it is not that simple to use the timer. If you can't find the ontimer () method in the class message map, there is no hwnd in the class, settimer () can not be used as it is. The method of using the timer can be used in a clever use of the integrity of the class.

2. Related knowledge

Using timers in non-window classes, you need to know more knowledge. First, there is no message map in the window class, and there is no settimer () method like the CWND class to set the timer. Without message mapping, you can only process the timer's message by the callback function defined by our own, so everyone must understand the concept of callback function. Because the callback function can only be implemented with global functions or static member functions, in order to maintain the integrity of the class, use the static member function of the class as a callback function, so we need to understand the nature of static data members and static member functions. Also because the timer is generated in our program, this needs to manage the timer, so the mapping table CMAP is used, so it is essential to introduce the simple usage of the CMAP.

2.1 callback function

The so-called callback function is to define and write implementations according to certain forms, and when some event occurs, the function is called by the system or other functions.

When using the callback function is actually called a function (usually an API function), the address of a function (that is, the callback function) is passed to that function as a parameter. When the function is needed, it is the time when something is happening, and the transfer function address is used to call the callback function. At this time you can use this opportunity to handle messages in the callback function or complete certain operations. The callback function can only be a global function, or a static function, because this function is only used in this class, so in order to maintain the integrity of the class, we use the static member functions of the class to make a callback function. 2.2 static members in C classes

In the C language, declare a data is a static type, meaning that the volume of the variable is static, that is, allocated at the beginning of the program, and release it when the program is terminated. However, in C , the members in a class are static, which means that all instances of the class have only a copy of the member. That is, regardless of how many objects are created in the application, their static members have only one copy, which is shared by all object instances of this class, and for non-statist members, each class object instance has yourself Copy. E.g:

Class CPERSON

{

PUBLIC:

Cstring szname;

Static cstring szcompanyname;

CPERSON ();

Virtual ~ CPERSON ();

}

Then declare an example CPERSON ME with this class;

For employees of the same company, everyone has different names, but their names are the same, so they can be saved with a static type, so all employees share the name of this company, as long as an employee updated the company Name, all employees' company names are updated.

Static members are treated as a global object of such types, which can store and access a static data member and static member functions as global variables and functions, but are hidden in the interior of the class and clearly associated with this class. But it is not a global object, compared with the global object, there are two advantages of using static members:

(1) Static members do not enter the global namespace of the program, it belongs to the class, and its name is only valid within the scope of the class, so there is no possibility of conflict with other global names in the program.

(2) You can hide information, and you can maintain the integrity of the class, which can be a private member, public (public) member or protected member, and global objects cannot be.

2.2.1 Static Data Member

Using static data members can save memory because it is public, so that multiple objects are used, and the static data member is only stored for all objects. The value of the static data member is the same, but its value is updated. As long as the value of the static data member is updated once, you can guarantee that all objects can access the updated value, which improves efficiency and saving memory space.

In the class, a member variable is declared as static, and the only difference between declating normal variables is to add a Static before it is defined.

Declaring in the example above:

Static cstring szcompanyname.

Static data members explicitly initialize differently in initialization of general data members. The format of the static data member explicitly initialized is as follows:

:: =

For example, the example is initialized:

CString CPERSON :: SZCommpanyName = "Net into Technology",

This indicates that: (1) Initialization is carried out outside the class, while the front is not added to avoid confusion with the general static variable or object.

(2) When initialization, the member's access control privat, public, etc.

(3) When initialization, use the scope operator to indicate it, and therefore, static data members are members of the class, not members of the object.

The static data member of this class can be directly referenced in the member function of the class without having to use the member to access the operator. But in non-member functions, we must access one or two ways to access static data members.

(1) Use member access operators.

For example: ME is an instance of CPERSON, which can be applied in the non-member function:

CString thecommpanyname = me.commpanyname.

(2) Because the static data members have only one copy, it does not have to access the object or pointer. Method 2 is to directly access it with the name that is defined by the class name. When we do not access a static data member through a member access operator, you must specify a class name and a domain operator that followed, because static members are not global objects, so we can't find it in the global domain. Such as:

CString ThecommpanyName = CPERSON :: CommpanyName.

By the way, there are two characteristics of static data members:

(1) The type of static data member can be its belongs, rather than the static data member can only be declared a pointer or reference of the object of the class.

(2) Static data members can be used as the default arguments of the class member function, not the static members.

2.2.1 Static member functions

The unique difference between static member functions and ordinary functions is to add a Static in front.

Typically, the address of the current object (THIS) is hiddenly passed to the called non-active member function. The static member function has the range of classes, compared with the non-static member function, the static member function does not have this parameters, so it cannot access a general data member, but only access to static data members, enumerations or nested types and other static Member function. This uses a static member function to have a little growth in the speed than the global function, which not only does not deliver the extra cost of the THIS pointer, but also benefits the function in the class. If you want to reference a non-statistial member in a static member function, you can reference the object.

We can access a static member function with a member access operator point (.) And arrow (->) to a class object or a pointer to the key object, or you can use the defined modification to directly access the static member function without claiming classes.

Static member functions follow the constraints:

(1) Access non-statist members cannot be accessed with member selectors (. Or ->).

(2) It cannot be explained as a virtual function.

(3) Can't be the same as a non-static member letter with the same parameter type.

(4) Cannot be declared as const or volatile.

(5) A function of the function outside the class does not specify a keyword Static

2.3 Mapping table CMAP

Mapping Table (CMAP) is a template class in the MFC collection class, also known as "Dictionary", just like a table with only two columns, a column is a keyword, a column is a data item, which is one or one. The keyword is unique, gives a keyword, mapping table class will quickly find the corresponding data item. The lookup of the mapping table is carried out in a hash table, so the speed of finding the value in the mapping table is very fast. For example, all the staff of the company have a working number and his name, the work number is the keyword named, give a work number, you can quickly find the corresponding name. The mapping class is most suitable for applications that need to be quickly retrieved according to the keywords. In our program, the mapping table is used to save the timer flag value and class instance pointer, with the timer's flag value as the keyword.

3. Let the static member function can also access the non-static member function. From the above description, the static member function can only reference static data members and static member functions, how to make static member functions can also reference non-static Member function and member variable? This is also what we will use later.

Analyze the distinction between static member functions and non-static member functions, we will find that the non-statistial member function can access all member functions and members variables because it has an implicit parameter this, access member function and member variables. Time, actually add a referenced symbol "this->" in front, so we can try this pointer as a parameter of the static member function, so you can access all in a static member function. Is the member function and a member variable? Here is an example of an implementation:

The Person.h file is as follows:

Class CPERSON

{

PUBLIC:

/ / A sentence in this example

Cstring szmotto;

// The pointer for saving this instance

CPERSON * PTHIS;

// Non-static member function, pop up the motto of the instance

Void getmotto ();

// Static member function, pop up the motto of the instance

Static void getMottostAIC (CPERSON * PPERSON);

CPERSON ();

Virtual ~ CPERSON ();

}

The Person.cpp file is as follows:

#include "stdafx.h"

#include "person.h"

CPERSON :: CPERSON ()

{

PTHIS = THIS;

}

CPERSON :: ~ CPERSON ()

{

}

Void CPERSON :: getmotto ()

{

AfxMestageBox (Szmotto);

}

Void CPERSON :: getMottostaic (CPERSON * PPERSON)

{

PPERSON-> getmotto ();

}

You can access static member functions as needed.

m_person.szmotto = "My motto is: This is the result of accessing the non-static function by static functions!";

M_Person.getMottostAIC (m_person.pthis);

In fact, this example is actually there is no significance, so the purpose is just to demonstrate how to implement this method.

转载请注明原文地址:https://www.9cbs.com/read-26193.html

New Post(0)