The method of using timer in non-window class and the introduction of relevant knowledge

Source: Internet
Author: User
Tags map class time interval

The code for this article runs the following chart:

Absrtact: This article mainly introduces how to use timers in Visual C + + windows and non-window classes, with a few simple examples. This paper focuses on how to use static member functions and static data members in non window classes, and also introduces timer-related knowledge, such as callback function, static member in C + + class, and mapping class in template class.

Keywords C + + class Timer static function static member function static data member callback function mapping class

Summary: This page introduce how to use Timer in window class and none window Class of Visual C + + by some simple samples. Use timer in the ' None window class with static ' variable and static member function ' the important point. At the same time, it also tell about 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

First, the introduction

The role of timers in Windows programs cannot be ignored and can be seen everywhere. Set a time interval every 0.5 seconds or 1 seconds to refresh the clock, so that you can complete a simple electronic clock program. Timers are used differently in different programming tools, and Visual C + + provides us with a way to implement this functionality, with more than one method. In the window class is very simple to use the timer, after using SetTimer () set the timer, and in Class Wizard added OnTimer message mapping, you can in the mapping function OnTimer () to add code implementation, to complete your task at timed, It also supports any number of timers that you might use. But in a windowless class, using the timer is not so simple, in the class message map can not find the OnTimer () method, the class also has no HWND this property, SetTimer () can not be used as the original, Here is a way to use the timer skillfully without destroying the integrity of the class.

Ii. relevant Knowledge

Using timers in a non-window class requires 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 a message map, we can only handle the timer message by our own defined callback function, so it is important to understand the concept of the callback function. Because callback functions can only be implemented with global functions or static member functions, in order to maintain the integrity of the class, using static member functions of the class as callback functions, we need to understand the nature of static data members and static member functions. And because the timer is generated in our program, which needs to manage the timer, so we used the mapping table class CMAP, so the introduction of CMap simple usage is also essential.

2.1 Callback function

A callback function is a function that is defined and written by you in a certain form that is called by a system or other function when an event occurs.

Using a callback function is actually a function (usually an API function) that passes the address of one of its written functions (that is, the callback function) as an argument to that function. And that function, when needed, when something happens, invokes the callback function with the function address of the pass, at which point you can use this opportunity to process the message in the callback function or to complete a certain operation. A 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 function of the class as the callback function.

2.2 Static members in the C + + class

In C, declaring a data as a static type means that the lifetime of the variable is static, that is, allocated at the beginning of the program, and released when the program terminates. In C + +, however, declaring a member in a class to be a static type means that all instances of the class have only one copy of that member. That is, no matter how many objects of the class are created in the application, there is only one copy of the static member, which is shared by all object instances of the class, and for Non-static members, each class object instance has its own copy. For example:

class CPerson
{
public:
  CString szName;
  static CString szCompanyName;
  CPerson();
  virtual ~CPerson();
};

Then use this class to declare an instance CPerson me;

For employees of the same company, everyone has a different name, but their company name is the same, so it can be saved with a static type, so that all employees share the company name, so long as one employee updates the company name, the company name of all employees is updated.

A static member is treated as a global object of that class type. You can store and access a static data member and a static member function as global variables and functions, but they are hidden within the class, and are clearly associated with this class but are not global objects, and there are two advantages to using static members compared to global objects:

(1) The static member has no access to the global namespace of the program, it belongs to the class, 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) Information hiding can be realized and the integrity of the class can be maintained, which can be private (private) members, public (common) members, or protected (protected) members, and global objects cannot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.