C + +-based class calling method and device, electronic equipment and storage medium
By designing class managers and signal and slot interfaces, the problem of complex class instantiation and calling relationships is solved, the software system is simplified and modularized, and the development and maintenance process is simplified.
Patent Information
- Application Number
- CN202410414369.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-08
- Publication Date
- 2024-08-23
AI Technical Summary
In software design based on C++, the instantiation and calling relationships of classes are complex, resulting in a messy program architecture that is difficult to read and maintain.
The design class manager manages class objects in a unified manner, and connects the calling relationships between classes through signal and slot interfaces, simplifying the generation and calling of class objects, and achieving simplification and modularization of the system architecture.
Through the class manager and signal and slot interfaces, the management and invocation of class objects are simplified, the difficulty of code development is reduced, and the maintainability and simplicity of the software system are improved.
Smart Images

Figure FT_1 
Figure FT_2 
Figure FT_4
Abstract
Description
Technical Field
[0001] The present application relates to the field of software design technology, and in particular to a C++-based class calling method, device, electronic device and storage medium. Background Art
[0002] In the process of software design based on C++, modular development is usually carried out. Each module is encapsulated into a C++ class, which encapsulates member variables and member functions. When the software is running, the class objects are dynamically instantiated, and various data processing and logical relationship operations are performed through the objects.
[0003] For system developers, the following problems are usually encountered: 1. When are each class instantiated? 2. How to connect the calling relationships between class objects when each class collaborates to complete a task? The general practice is to instantiate the required class in the main function, or dynamically instantiate the class object when the trigger condition is met. However, the problem caused by this is that it is difficult to find the location of the object in other places where the object is needed. For calls between objects, the general practice is to pass the object pointer as a parameter to the parameter list of the function that needs to use the object. This method is very messy and difficult to read and maintain for the overall architecture of the program.
[0004] Therefore, the existing technology is in urgent need of improvement. Summary of the invention
[0005] In view of the above-mentioned shortcomings of the prior art, the present application provides a C++-based class calling method, device, electronic device and storage medium, which are applied to the field of software design technology. By designing a class manager to uniformly manage all classes, and by designing signal and slot interfaces, the calls and data synchronization between classes are connected, so as to solve the problem that the program architecture of class calls in the prior art is messy and difficult to read and maintain, and realize the simplification and modularization of the system architecture of embedded software, which is convenient for technical personnel to develop and maintain.
[0006] In a first aspect, the present application provides a class calling method based on C++, the method comprising the following steps: A class calling method based on C++, the method comprising the steps of: S1: designing a C++-based class manager, wherein the C++ class manager at least includes an init member function, a first function class to be called, and a second function class interacting with the first function class; S2: Encapsulating the interface of signals and slots designed based on C++ in the class manager; S3: In the init member function, the first function class and the second function class are instantiated, and the objects in the first function class are connected with the objects in the second function class through the signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0007] The present application provides a C++-based class calling method, which designs a C++-based class manager for unified management of class objects and unified connection of class objects, so that classes other than the class manager no longer need to consider the problem of interaction with objects of other classes, thereby simplifying the architecture of the software system; encapsulating the signal and slot interface designed based on C++ in the class manager can facilitate the class manager to connect objects of various classes through the signal and slot interface, making it convenient for an object of one class to call an object of another class, thereby completing a software task. The class manager includes at least the init member function, the first function class that needs to be called, and the second function class that interacts with the first function class. In the init member function, the first function class and the second function class can be instantiated. The instantiation process is the process of creating the first function class and the second function class, and connecting the objects in the first function class with the objects in the second function class through the pre-packaged signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class, realizing the generation and calling relationship of unified class objects, effectively reducing the difficulty of code development, and realizing the simplification and modularization of the system architecture of embedded software, which is easy to develop and maintain.
[0008] Furthermore, step S1 includes: the class manager is a class that inherits the singleton pattern.
[0009] The present application provides a C++-based class calling method, which sets the class manager as a class that inherits the singleton pattern, so that the class manager can be obtained as a global pointer at any position in the program, and then the member function of the class to be called can be called through the member function of the class manager.
[0010] Further, step S2 includes: S21: Encapsulating the signal and slot interface through a template class function in the class manager; S22: Bind the encapsulated signal and slot interface to the second function class that needs to interact with the object of the first function class.
[0011] The present application provides a C++-based class calling method, in which the interfaces of signals and slots can be encapsulated through a template class to facilitate integration into a second function class, and the encapsulated interfaces of signals and slots are bound to the second function class that needs to interact with objects of the first function class. Thus, each class only needs to focus on its own function implementation, and through the interfaces of signals and slots, calls and data synchronization between classes can be realized, which simplifies the program of the first function class, realizes the generation and calling relationship of objects of a unified class, effectively reduces the difficulty of code development, and realizes the simplification and modularization of the system architecture of embedded software, which is convenient for development and maintenance.
[0012] Further, step S22 includes: S221: Define a connection macro CONNECT to bind the interface of the signal and slot to the object in the second function class.
[0013] The present application provides a C++-based class calling method, which defines a connection macro CONNECT, which is used to bind the interface of signals and slots to objects in a second function class, wherein the connection macro CONNECT is a global static variable. Any class in the second function class can be connected to another class through the connection macro CONNECT, thereby realizing the unified calling function and data synchronization function of the second function class and the first function class.
[0014] Further, step S221 includes: S222: Bind the objects in the second function class, the slot function interface, and the objects in the first function class passed from the signal function interface, and put them into the m_pSlotSetbind array; S223: The m_pSlotSetbind array is defined as a private member of the interface between signals and slots; S224: Using the interface of the signal and the slot as a parameter of the connection macro CONNECT, and binding it to the object in the second function class.
[0015] Furthermore, before step S3, the following steps are included: S30: Define data input class functions and data output class functions in the first function class.
[0016] Further, step S3 includes: S31: In the init member function, the data input class function and the data output class function are instantiated, so that the data input class function and the data output class function are used to implement the signal function interface and the slot function interface in the signal and slot interface respectively; S32: Bind the signal function interface and the slot function interface, so that the objects in the first function class are connected with the objects in the second function class through the signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0017] In a second aspect, the present application provides a class calling device based on C++, the device comprising: Design module: used for designing a C++-based class manager, wherein the C++ class manager at least includes an init member function, a first function class to be called, and a second function class interacting with the first function class; Encapsulation module: used to encapsulate the signal and slot interface designed based on C++ in the class manager; Connection module: used to instantiate the first function class and the second function class in the init member function, and connect the objects in the first function class with the objects in the second function class through the signal and slot interface, to ensure that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0018] In a third aspect, the present application provides an electronic device, comprising a processor and a memory, wherein the memory stores computer-readable instructions, and when the computer-readable instructions are executed by the processor, the steps in the method provided in the first aspect are performed.
[0019] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, runs the steps in the method provided in the first aspect above.
[0020] Beneficial effects: The present application provides a C++-based class calling method, device, electronic device and storage medium, which, by designing a C++-based class manager, is used to uniformly manage class objects and uniformly connect class objects, so that classes other than the class manager no longer need to consider the problem of interacting with objects of other classes, thereby simplifying the architecture of the software system; encapsulating the signal and slot interface designed based on C++ in the class manager can facilitate the class manager to connect objects of various classes through the signal and slot interface, making it convenient for objects of one class to call objects of another class, thereby completing software tasks. The class manager includes at least the init member function, the first function class that needs to be called, and the second function class that interacts with the first function class. In the init member function, the first function class and the second function class can be instantiated. The instantiation process is the process of creating the first function class and the second function class, and connecting the objects in the first function class with the objects in the second function class through the pre-packaged signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class, realizing the generation and calling relationship of unified class objects, effectively reducing the difficulty of code development, and realizing the simplification and modularization of the system architecture of embedded software, which is easy to develop and maintain. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 A flow chart of a C++ based class calling method proposed in this application.
[0022] Figure 2 This is a schematic diagram of the structure of a C++ based class calling device proposed in this application.
[0023] Figure 3 This is a schematic diagram of the structure of an electronic device proposed in this application.
[0024] Figure 4 This is a calling relationship diagram between the class manager and the first function class and the second function class proposed in this application.
[0025] Figure 5 This is the program running result of a C++ based class calling method proposed in this application.
[0026] Explanation of reference numerals: 3, electronic device; 301, processor; 302, memory; 303, communication bus; 201, design module; 202, packaging module; 203, connection module. DETAILED DESCRIPTION
[0027] The technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. The components of the embodiments of the present application usually described and marked in the drawings here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.
[0028] It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in subsequent drawings. At the same time, in the description of this application, the terms "first, second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.
[0029] The following disclosure provides many different implementations or examples to achieve the purpose of the present invention and solve the problems existing in the prior art. At present, in the process of embedded software development based on C++, there are problems such as inconsistent object management and complex and messy calling relationships between objects, which make the program difficult to read and maintain. In order to solve this problem, the present application provides a class calling method, device, electronic device and storage medium based on C++, specifically: Please refer to Figure 1 , the embodiment of the present application provides a class calling method based on C++, the method comprising the following steps: S1: Design a C++-based class manager, where the C++ class manager at least includes an init member function, a first function class that needs to be called, and a second function class that interacts with the first function class; S2: Encapsulate the signal and slot interface designed based on C++ in the class manager; S3: In the init member function, the first function class and the second function class are instantiated, and the objects in the first function class are connected with the objects in the second function class through the signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0030] In step S1, the class manager is actually a class designed based on C++, which is used to implement the instantiation and unified management functions of other classes. The class manager itself is instantiated in the main function main, avoiding the problem in the prior art that all classes are instantiated in main, which leads to difficulties in code reading and maintenance. The instantiation of a class refers to the process of creating an object of a class, and creating space in static storage for the object of the class (the object is the concrete embodiment of the class, and the class is a description of the specific object).
[0031] Among them, the process of instantiating the class manager in the main function main is: #include "classManager.h" int main(int argc, char *argv[]) { classManager* m_classManager = new classManager(); m_classManager->init(); return 0; } The class manager is defined as: / / cpp file #include "classManager.h" #include "a_send.h" #include "b_receive.h" classManager::classManager() { } void classManager::init() { m_a_send = new A_send(); m_b_receive = new B_receive(); CONNECT(m_a_send,get_num,m_b_receive,&B_receive::cal_num); / / Test code m_a_send->test(); } The class manager at least includes an init member function, a first function class to be called, and a second function class that interacts with the first function class, wherein the init member function is a constructor, which is used to instantiate the first function class and the second function class, and assign initial values, so that the first function class and the second function class are centralized in the class manager for unified management. The first function class and the second function class may include multiple different classes, such as the attached Figure 4 As shown, the first function class may include Class 1, Class 2, Class 3, Class 4, Class 5, etc., depending on the actual situation of the code architecture, and the second function class is the same. The first function class is the class that needs to be called, and the second function class is the class that interacts with the first function class. Each class in the first function class and the second function class only focuses on realizing its own functions, and does not consider the connection relationship with other classes. Specifically, the first function class realizes the functions required by the software system, and the second function class realizes the calling function of each class in the first function class.
[0032] In some preferred implementations, in order to implement a unified calling function for each class, it is necessary to be able to obtain a pointer to the object of the class manager at any location in the program. Therefore, further, step S1 includes: the class manager is a class that inherits the singleton pattern.
[0033] Among them, the singleton mode is a class that has only one instance in the current process. Specifically, the design process of the singleton mode Usington.h is as follows: #ifndef USINGLETON_H #define USINGLETON_H #include<assert.h> template<typename T> class USingleton { public: static T&Instance(void) { static T m_singleton; return m_singleton; } public: USingleton( void ){} ~USingleton( void ){} }; #endif / / USINGLETON_H The process of class manager inheriting the singleton pattern is as follows: #ifndef CLASSMANAGER_H #define CLASSMANAGER_H #include "USingleton.h" class classManager:public USingleton <classmanager> { public: classManager(); public: void init(); }; #endif / / CLASSMANAGER_H Among them, classManager is the class manager, init() is the init member function in the class manager, and the init member function instantiates the first function class and the second function class. By setting the class manager to a class that inherits the singleton pattern, the object of the class manager can be obtained as a global pointer at any position in the program, and then the member function in the class to be called can be called through the member function of the class manager.
[0034] Among them, in step S2, in order to facilitate the unified calling and data synchronization of each class, the signal and slot interface designed based on C++ can be encapsulated in the class manager, wherein the signal and slot interface includes a signal function interface and a slot function interface, the signal function interface includes a signal object get_num(int,int), and the slot function interface includes a slot object cal_num(int,int), wherein int is a data type, the signal object is bound to the slot object, and the connection between the signal function interface and the slot function interface is realized to form a signal and slot interface.
[0035] Further, step S2 includes: S21: Encapsulate the interface of signals and slots through template class functions in the class manager; S22: Bind the encapsulated signal and slot interface to the second function class that needs to interact with the object of the first function class.
[0036] In actual applications, the interfaces of signals and slots are encapsulated through template class functions, where the encapsulation implementation process is: template<class... ARGS_T> class Signal { public: Signal() {} template<class T> void Bind(T* pObj, void (T::*func)(ARGS_T...)) { auto t = new Slot<ARGS_T...> (pObj, func); m_pSlotSet.push_back(t); } ~Signal() { for (int i = 0; i<(int)m_pSlotSet.size(); i++) { delete m_pSlotSet[i]; } } void operator()(ARGS_T... param1) { for (int i = 0; i<(int)m_pSlotSet.size(); i++) { m_pSlotSet[i]->Exec(param1...); } act(); } void act() { for (int i = 0; i<(int)m_pSlotSet.size(); i++) { m_pSlotSet[i]->act(); } } private: std::vector <Slot<ARGS_T...> *>m_pSlotSet; }; Among them, Signal is the class of the interface between signals and slots. The above code also implements the binding function Bind() and the overloaded operator function operator(); the function act() uses the parameters passed by the signal object as the parameters of the slot function interface to execute; m_pSlotSet is a private member variable of Signal.
[0037] Among them, the parameters of the binding function Bind() are the object of the class to which the signal function interface is to be bound, and the member function of the object. Whenever the binding function Bind() is called, a new Slot object is dynamically generated and placed in the private member variable m_pSlotSet.
[0038] When there is an object of a class to which a signal function interface is to be bound, such as getNum, when getNum is called, the overloaded operator function operator() will be called. The overloaded operator function operator() will poll the Slot objects in the m_pSlotSet array and call the process control function Exec of the Slot object.
[0039] Among them, the class definition of the Slot object is as follows: template<class... ARGS_T> class Slot:public SlotBase<ARGS_T...> { public: template<class T> Slot(T* pObj, void (T::*func)(ARGS_T...)) { m_pSlotBase = new SlotImpl<T, ARGS_T...> (pObj, func); } ~Slot() { delete m_pSlotBase; } void Exec(ARGS_T... param1) { m_pSlotBase->Exec(param1...); } void act() { m_pSlotBase->act(); } bool isEmpty() { return m_pSlotBase->isEmpty(); } private: SlotBase<ARGS_T...> * m_pSlotBase; }; Among them, the class instantiated by the Slot object in the init member function is SloImpl, and the specific process is as follows: template<class T, class... ARGS_T> class SlotImpl : public SlotBase<ARGS_T...> { using bind_type = decltype(std::bind(std::declval <std::function<void(ARGS_T...)> >(), std::declval<ARGS_T> ()...)); public: SlotImpl(T* pObj, void (T::*func)(ARGS_T... param1)) { m_pObj = pObj; m_Func = func; } void Exec(ARGS_T... param1) { auto yy = std::bind(m_Func, m_pObj, std::forward<ARGS_T> (param1)...); bind_type _bind(yy, std::forward<ARGS_T> (param1)...); m_pSlotSetbind.push_back(_bind); } void act() { for (int i = 0; i<(int)m_pSlotSetbind.size(); i++) { m_pSlotSetbind[i](); } m_pSlotSetbind.clear(); } bool isEmpty() { return m_pSlotSetbind.size()>0 ? false : true; } private: std::vector<bind_type> m_pSlotSetbind; private: T* m_pObj; void (T::*m_Func)(ARGS_T... param1); }; Among them, SlotImpl has two member variables m_pObj and m_Func, which respectively store the object of the class bound to the signal function interface and the slot function interface of the object.
[0040] Among them, SlotImpl inherits the SlotBase class, the specific process is: template<class... ARGS_T> class SlotBase { public: virtual void Exec(ARGS_T... param1) = 0; virtual void act() = 0; virtual bool isEmpty() = 0; }; Exec(ARGS_T...param1), act(), and isEmpty() are three virtual functions. Exec(ARGS_T...param1) means creating and running a new program to bind the objects in the second function class, the slot function interface, and the objects of the first function class passed from the signal function interface; act() is used to execute with the parameters passed by the signal object as the parameters of the slot function interface; isEmpty() is a template class used to determine whether the object instantiated in the init member function is empty.
[0041] Therefore, further, step S22 includes: S221: Define the connection macro CONNECT to bind the interface of the signal and slot to the object in the second function class.
[0042] The process of defining the connection macro CONNECT is: #define CONNECT( sender, signal, receiver, method) ( (sender)->signal.Bind(receiver, method) ) By defining the connection macro CONNECT, the interface of the signal and slot is bound to the object in the second function class. In each class of the second function class, CONNECT can be used as a global variable, so that the objects of each class in the second function class can be called arbitrarily in the program.
[0043] Specifically, step S221 includes: S222: Bind the objects in the second function class, the slot function interface, and the objects of the first function class passed from the signal function interface, and put them into the m_pSlotSetbind array; S223: The m_pSlotSetbind array is defined as a private member of the interface between signals and slots; S224: Use the interface of the signal and the slot as the parameter of the connection macro CONNECT and bind it to the object in the second function class.
[0044] From the above code, we can see that after executing Bind(), when calling the object of the class to which the signal function interface is bound, such as getNum, the Exec function of the Slot object will be called, and then the Exec function of SlotImpl will be called, and finally the act function of SlotImpl will be called. The Exec function of SlotImpl will bind the objects in the second function class, the slot function interface, and the objects of the first function class passed from the signal function interface together and put them into the m_pSlotSetbind array. The act function will be executed with the parameters passed by the signal object as the parameters of the slot function interface.
[0045] In this way, the calling relationship binding between the second function class objects is realized by designing the interface of signals and slots.
[0046] Among them, in step S3, the first function class Class A_send and the second function class class B_receive are instantiated, where Class A_send and class B_receive are class naming descriptions. In actual applications, it can be determined according to specific circumstances. Class A_send realizes the connection with the signal object get_num(int, int) of the signal function interface; classB_receive realizes the connection with the slot object cal_num(int, int) of the slot function interface, where int is a data type, and the data type is an integer (here the data type is defined as an integer only for example, and it can be determined according to specific circumstances in actual applications). In the class manager, the signal object and the slot object are bound together. Through the calling function of the connection object of the slot and signal interface, A_send in this embodiment can pass in two integers a and b, such as a=2, b=3; in B_receive, the sum of the two integers is calculated and printed out. The specific process is: Instantiate A_send: / / h file #ifndef A_SEND_H #define A_SEND_H #include "UObject.h" class A_send { public: A_send(); public: Signal<int,int> get_num; public: void test(); }; #endif / / A_SEND_H / / cpp file A_send::A_send() { } void A_send::test() { get_num(2,3); } Instantiate B_receive: / / h file #ifndef B_RECEIVE_H #define B_RECEIVE_H #include "UObject.h" class B_receive { public: B_receive(); public: void cal_num(int,int); }; #endif / / B_RECEIVE_H / / cpp file B_receive::B_receive() { } void B_receive::cal_num(int a,int b) { std::cout<<"the res sum of a and b is:"< <a+b<<std::endl; } In practical applications, in order to ensure that the first function class can be connected to the interface of signals and slots, specifically, before step S3, the following steps are included: S30: Define data input class functions and data output class functions in the first function class.
[0047] Among them, the first function class, while completing its own function, also needs to have an output (OUT) interface and an input (IN) interface. The data input class function is IN, and the data output class function is OUT. This function is a commonly used function in the prior art and will not be described in detail here.
[0048] Further, step S3 includes: S31: in the init member function, instantiate the data input class function and the data output class function, so that the data input class function and the data output class function are used to implement the signal function interface and the slot function interface in the signal and slot interface respectively; S32: Bind the signal function interface and the slot function interface, so that the objects in the first function class are connected with the objects in the second function class through the signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0049] Among them, in actual applications, in order to enable the data input class function and the data output class function to be implemented, it is necessary to instantiate them in the init member function, and use the data input class function and the data output class function to implement the signal function interface and the slot function interface in the signal and slot interface respectively. For details, please refer to the attached Figure 4 The connection relationship in the first function class is established, and then the signal function interface is bound to the slot function interface, that is, the signal object in the above embodiment is bound to the slot object, and the slot object is bound to the object of the second function class. Therefore, through this connection relationship, the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class. For example: A_send passes in two integers a and b, such as a=2, b=3; in B_receive, the sum of these two integers is calculated, and the output result can be referred to in the attached Figure 5 .
[0050] It can be seen from this that the present application provides a C++-based class calling method, which designs a C++-based class manager to uniformly manage class objects and uniformly connect class objects, so that classes other than the class manager no longer need to consider the problem of interaction with objects of other classes, thereby simplifying the architecture of the software system; encapsulating the signal and slot interface designed based on C++ in the class manager can facilitate the class manager to connect objects of each class through the signal and slot interface, making it convenient for an object of one class to call an object of another class, thereby completing the software task. The class manager includes at least the init member function, the first function class that needs to be called, and the second function class that interacts with the first function class. In the init member function, the first function class and the second function class can be instantiated. The instantiation process is the process of creating the first function class and the second function class, and connecting the objects in the first function class with the objects in the second function class through the pre-packaged signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class, realizing the generation and calling relationship of unified class objects, effectively reducing the difficulty of code development, and realizing the simplification and modularization of the system architecture of embedded software, which is easy to develop and maintain.
[0051] In a second aspect, a class calling device based on C++, the device comprises: Design module 201: used to design a C++-based class manager, where the C++ class manager at least includes an init member function, a first function class to be called, and a second function class interacting with the first function class; Encapsulation module 202: used to encapsulate the signal and slot interface designed based on C++ in the class manager; Connection module 203: used to instantiate the first function class and the second function class in the init member function, and connect the objects in the first function class with the objects in the second function class through the signal and slot interface, to ensure that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0052] Among them, in actual application, the design module 201, the packaging module 202, and the connection module 203 are all program modules running in the embedded software system.
[0053] The design module 201 can be used to design a C++ based class manager, which includes at least an init member function, a first function class to be called, and a second function class that interacts with the first function class, wherein the init member function is a constructor, which is used to instantiate the first function class and the second function class, and assign initial values, so that the first function class and the second function class are centralized in the class manager for unified management. The first function class and the second function class may include multiple different classes, for details, refer to Figure 4 As shown, the first function class may include Class 1, Class 2, Class 3, Class 4, Class 5, etc., depending on the actual situation of the code architecture, and the second function class is the same. The first function class is the class that needs to be called, and the second function class is the class that interacts with the first function class. Each class in the first function class and the second function class only focuses on realizing its own functions, and does not consider the connection relationship with other classes. Specifically, the first function class realizes the functions required by the software system, and the second function class realizes the calling function of each class in the first function class.
[0054] Among them, in actual applications, the class manager is a class that inherits the singleton pattern. By setting the class manager to a class that inherits the singleton pattern, the object of the class manager can be obtained as a global pointer at any position in the program, and then the member function of the class to be called can be called through the member function of the class manager.
[0055] Among them, in actual applications, in order to facilitate the unified calling and data synchronization of various classes, the encapsulation module 202 can encapsulate the signal and slot interface designed based on C++ in the class manager, wherein the signal and slot interface includes a signal function interface and a slot function interface, the signal function interface includes a signal object get_num(int, int), and the slot function interface includes a slot object cal_num(int, int), wherein int is a data type, the signal object is bound to the slot object, and the connection between the signal function interface and the slot function interface is realized to form a signal and slot interface.
[0056] Among them, in actual application, the connection module 203 instantiates the first function class Class A_send and the second function class class B_receive in the init member function, wherein Class A_send and class B_receive are class naming descriptions. In actual application, it can be determined according to the specific situation. Class A_send realizes the connection with the signal object get_num(int, int) of the signal function interface; class B_receive realizes the connection with the slot object cal_num(int, int) of the slot function interface, wherein int is a data type, and the data type is an integer (the data type is defined as an integer here just for example, and it can be determined according to the specific situation in actual application). In the class manager, the signal object and the slot object are bound together. Through the calling function of the connection object of the slot and signal interface, A_send in this embodiment can pass in two integers a and b, such as a=2, b=3; the sum of the two integers is calculated in B_receive and printed out.
[0057] It can be seen that the present application provides a C++-based class calling device, which designs a C++-based class manager for unified management of class objects and unified connection of class objects, so that classes other than the class manager no longer need to consider the problem of interaction with objects of other classes, thereby simplifying the architecture of the software system; encapsulating the signal and slot interface designed based on C++ in the class manager can facilitate the class manager to connect objects of each class through the signal and slot interface, making it convenient for an object of one class to call an object of another class, thereby completing the software task. The class manager includes at least the init member function, the first function class that needs to be called, and the second function class that interacts with the first function class. In the init member function, the first function class and the second function class can be instantiated. The instantiation process is the process of creating the first function class and the second function class, and connecting the objects in the first function class with the objects in the second function class through the pre-packaged signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class, realizing the generation and calling relationship of unified class objects, effectively reducing the difficulty of code development, and realizing the simplification and modularization of the system architecture of embedded software, which is easy to develop and maintain.
[0058] Figure 3 A structural schematic diagram of an electronic device provided in an embodiment of the present application, the present application provides an electronic device 3, including: a processor 301 and a memory 302, the processor 301 and the memory 302 are interconnected and communicate with each other through a communication bus 303 and / or other forms of connection mechanisms (not marked), the memory 302 stores computer-readable instructions executable by the processor 301, when the electronic device is running, the processor 301 executes the computer-readable instructions to execute the method in any optional implementation of the above embodiment to achieve the following functions: design a C++-based class manager, the C++ class manager at least includes an init member function, a first function class to be called, and a second function class that interacts with the first function class; encapsulates a signal and slot interface designed based on C++ in the class manager; in the init member function, instantiates the first function class and the second function class, and connects the objects in the first function class with the objects in the second function class through the signal and slot interface, to ensure that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0059] An embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method in any optional implementation of the above embodiment is executed to achieve the following functions: a class manager based on C++ is designed, and the C++ class manager includes at least an init member function, a first function class to be called, and a second function class that interacts with the first function class; a signal and slot interface designed based on C++ is encapsulated in the class manager; in the init member function, the first function class and the second function class are instantiated, and the objects in the first function class are connected to the objects in the second function class through the signal and slot interface, to ensure that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
[0060] Among them, the computer-readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable red-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk or optical disk.
[0061] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0062] In addition, the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0063] Furthermore, the functional modules in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
[0064] In this document, relational terms such as first and second, etc. are used merely to distinguish one entity or operation from another entity or operation, but do not necessarily require or imply any such actual relationship or order between these entities or operations.
[0065] The above description is only an embodiment of the present application and is not intended to limit the scope of protection of the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the scope of protection of the present application.< / classmanager>
Claims
1. A class calling method based on C++, characterized in that: The method comprises the steps of: S1: designing a C++-based class manager, wherein the C++ class manager at least includes an init member function, a first function class to be called, and a second function class interacting with the first function class; S2: Encapsulating the interface of signals and slots designed based on C++ in the class manager; S3: In the init member function, the first function class and the second function class are instantiated, and the objects in the first function class are connected with the objects in the second function class through the signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
2. A C++ based class calling method according to claim 1, characterized in that: Step S1 includes: the class manager is a class that inherits the singleton pattern.
3. A C++ based class calling method according to claim 2, characterized in that: Step S2 includes: S21: Encapsulating the signal and slot interface through a template class function in the class manager; S22: Bind the encapsulated signal and slot interface to the second function class that needs to interact with the object of the first function class.
4. A C++ based class calling method according to claim 3, characterized in that: Step S22 includes: S221: Define a connection macro CONNECT to bind the interface of the signal and slot to the object in the second function class.
5. A C++ based class calling method according to claim 4, characterized in that: Step S221 includes: S222: Bind the objects in the second function class, the slot function interface, and the objects of the first function class passed from the signal function interface, and put them into the m_pSlotSetbind array; S223: The m_pSlotSetbind array is defined as a private member of the interface between signals and slots; S224: Using the interface of the signal and the slot as a parameter of the connection macro CONNECT, and binding it to the object in the second function class.
6. A C++ based class calling method according to claim 1, characterized in that: Step S3 includes: S30: Define data input class functions and data output class functions in the first function class.
7. A C++ based class calling method according to claim 6, characterized in that: Step S3 includes: S31: In the init member function, the data input class function and the data output class function are instantiated, so that the data input class function and the data output class function are used to implement the signal function interface and the slot function interface in the signal and slot interface respectively; S32: Bind the signal function interface and the slot function interface, so that the objects in the first function class are connected with the objects in the second function class through the signal and slot interface, ensuring that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
8. A class calling device based on C++, characterized in that: The device comprises: Design module: used for designing a C++-based class manager, wherein the C++ class manager at least includes an init member function, a first function class to be called, and a second function class interacting with the first function class; Encapsulation module: used to encapsulate the signal and slot interface designed based on C++ in the class manager; Connection module: used to instantiate the first function class and the second function class in the init member function, and connect the objects in the first function class with the objects in the second function class through the signal and slot interface, to ensure that the member function of any class in the first function class can call the member function of another class through the signal and slot interface on the second function class.
9. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores computer-readable instructions. When the computer-readable instructions are executed by the processor, the steps in the method according to any one of claims 1 to 7 are executed.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are executed.
