Interview Questions (QT + function pointer)

Source: Internet
Author: User

The interviewer asked: what strategies do you think can be used to replace the QT signal slot mechanism? Because signal-slot itself is a sequential execution process, the next statement of emit will be executed only after the slot is executed. Its policy is similar to function calling, but it sets up a large structure in qobject, which leads to a low execution efficiency, it is 10 times slower than the direct function call (the data you forget to read ). During Normal Project Creation: if possible, I generally use the pure virtual interface to replace signal-slot. The interviewer asked: Have you ever thought about using function pointers instead? Khan Yixian has never thought about it before, and function pointers are rarely used. Today I made up the following knowledge:

I. function pointers.

This should be distinguished by pointer functions.

①: A program is divided into code and data areas, so a pointer can point to both the data zone where data is stored in the memory and the code zone where the code is stored. Each function is stored in the code area. We can use a pointer to point to a function.

②: The C Language stipulates that a function always occupies a contiguous memory zone, and the function name is the first address of the memory zone occupied by the function. We can assign the first address of the memory area occupied by this function to a pointer variable so that this pointer variable points to this function! Then we can use this pointer variable to locate and call the function.

The pointer variable pointing to the function is the "function pointer"

 

The general definition form of function pointers is:

Function return value type (* function pointer name) (List of function parameters)

For example: int (* Pmax) (INT, INT); this defines a function pointer that points to an int value containing two int parameters.

For example, the int max (int A, int B) Function)

We can call it like this: int as = max (a, B); // This is called by function name

We can also call it like this: int (* Pmax) (INT, INT); // call with function pointer

Pmax = max;

Int as = (* Pmax) (A, B );

 

Note: A function pointer is a pointer variable pointing to a function. It can only point to the function entrance, but not to a certain instruction in the middle of the function! Therefore, you cannot use the * (Pmax + 1) method to execute the next instruction of the function.

 

 

Ii. How to replace signal-slot with function pointers:

After knowing how the function pointer is returned, we can think about this problem.

The essence is how to use a function pointer to call a function. Obviously, you need to store all the function pointers, and then you can directly search for them when calling the function! I understand what the interviewer means:

①: Some slots are defined in a class. We can replace them as follows: First, some common member functions are defined (originally they should be slots ), then create another void function pointer array: store all the function pointers originally used as slot functions. This way: for a class, we replace slot with a function pointer array. (This array is used to distinguish between common member functions and class slot functions ).

②: For the connect () function and signal function, I have not come up with any good solutions. It seems that I can only use the observer interface. Directly call the corresponding function in the array of function pointers.

 

I think the key lies in: the class containing slots: If function pointers are used, we can directly declare the original slot functions as private member functions, however, opening this array to public can also reflect its differentiated advantages.

 

If you have any good idea, please let me know.

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.