Network extracted [3]

xiaoxiao2021-03-05  40

1. Use the constructor of the global object to call the function before the program starts

Some applications need to call other functions before starting the main program. For example: the transmissive process function, the registration function function must be called before the actual program is running. The easiest way is to call these functions through a constructor of a global object. Because global objects are constructed before the main program begins, these functions will return results before main (). Such as: class logger {public: logger () {activate_log (); // Translator Note: Call the function}}} you need to run in the constructor; Logger log; // A global instance int main () {record * PREC = read_log (); // Translator Note: Read the log file data // .. Program code} The global object log is constructed before the main () runs, and the log calls the function activate_log (). Therefore, when main () is executed, it can read data from the log file.

There is no doubt that memory management in C programming is the most complicated and easier to show bugs. Direct access to raw memory, dynamic allocation storage, and maximum playing C instruction efficiency, making you try to avoid BUG related to memory.

2, pointing to a member of the pointer

A class has two basic members: functions members and data members. Similarly, there are two pointers that point to members: pointing to the pointer of the function member and pointer to the data member. It is not commonly used because the class generally does not contain public data members, which is only used when the coordination structure and class (Class) are used when inheriting CLASS.

The pointer to the member is one of the most difficult constructs of the C syntax, but this is also a C most powerful feature. It allows you to call a class of function members without having to know the name of this function. This very agile calling tool. Similarly, you can also check and change this data without knowing its members' name by using a pointer to the data member. Pointer to point to data

Although the grammar of the pointer to the member will make you a little confused, but you will soon find that it is almost the same as ordinary pointers, but it is ahead of the * number :: Symbol and class name, Example: Define a pointer to the INT type: INT * Pi; Defines a data member to the INT type class: int A :: * PMI; // PMI is a INT type member pointing to class A You can initialize this It: Class a {public: int Num; int x;}; int A :: * pmi = & a :: num; the above code is a statement that points to an INT type NUM member to class A and initializes it to this Num members' address. By adding * you can use and change the value of the NUM member of Category A: A A1, A2; INT N = A1. * PMI; // Assign A1.Num to NA1. * PMI = 5; // Assign 5 to the A1.NUMA2. * PMI = 6; // Assign 6 assignment to 6A2.NUM If you define a pointer to class A, then the above operation You must use-> * Instead: a * pa = new a; int N = pa-> * PMI; Pa -> * PMI = 5;

Pointer to function member

It consists of the data type returned by the function member, and the class name followed by :: symbol, pointer name, and function's parameter list. For an example: a pointer to a function member of class A (this function returns an int type): Class a {public: int func ();}; int (a :: * pmf) (); the above definition is to say PMF is a pointer to the function member of the class A. In fact, this pointer and a pointer to a normal pointing function have no difference, just it contains the name of the class and :: symbol. You can call this function in any * PMF (): PMF = & a :: func; a a; (a. * Pmf) (); // call a.func () If you define one first Pointer to the object, then the above operation is used to use -> * instead: a * pa = & a; (pa-> * pmf) (); // Call PA-> FUNC () pointing to the pointer to the function member to consider polymorphism Sex. So, this call will be dynamically recycled when you call a virtual function member through a pointer. Another place to pay attention to, you can't take a class constructor and the address of the destructor. 3, DELETE is still delete []

There is an absurd statement in the programmer: use Delete instead of delete [] to delete an array type! For example: int * p = new int [10]; delete p; // error, it should be: delete [] p The above program is completely wrong. In fact, using delete instead of delete [] on a platform may not cause the system to crash, but that is purely luck. You can't guarantee that your app is compiled on another compiler, run on another platform, so please use delete [].

4, optimize the arrangement of members

The size of a class can be changed in the following manner: struct a {bool a; int b; bool c;}; // sizeof (a) == 12 on my computer SIZEOF (a) is equal to 12. This result may surprise you because the total number of members of A is 6 bytes: 1 4 1 byte. Where is the other 6 bytes? The compiler is inserted behind each BOOL member to ensure that each member is arranged in 4 bytes for the boundary. You can reduce the size of A, through the following: struct b {bool a; bool c; int b;}; // sizeof (b) == 8 this time, the compiler only inserts 2 bytes after the member C . Because B accounts for 4 bytes, it is naturally arranged as a word in the form of a word, and the size of A and C 1 1 = 2, plus 2 bytes just press two words. Form arrangement B.

5. Why inherited a class with no false argument function?

A class without a false aromacy means that it cannot be used as a base class. Such as std :: string, std :: intrlex, and std :: vector are all like this. Why inherited a class without a false argument function? When you have inheritively created a related class inherited from the base class, pointing to the pointer in the new class object and the reference actually points to the origin of the object. Because the destructor is not a virtual function, when you delete such a class, C will not call the destructor chain. For example: Class a {public: ~ a () // is not a virtual function {// ...}}; class b: public a // fault; A no false argument function {public: ~ b () {// ...}}; int main () {a * p = new b; // looks right delete p; // wrong, B's sectors are not called} 6, friends class Declare nested class

When you declare a nesting class with a friend class, the friend declared behind the nested category declaration, not the front. Class A {Private: INT I; Public: Class B // Nested Category Declaration in the Former {Public: B (A & A) {Ai = 0;};}; Friend Class B; // Friendly Class Declaration} If you put the friend class statement in front of the declaration nested class, the compiler will abandon other statements after the friend class.

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

New Post(0)