1. Placement New Method
For example, Pi = new (PTR) int; // placement new
The parameter in brackets is a pointer pointing to a memory buffer. The new operation will allocate an object to the buffer. The return value of placement new is the address of the constructed object (for example, the transfer parameter in the buckle ). Placement new is mainly applicable to: In applications with very high time requirements, because the time allocated by these programs is determined;
The Allocator space distributor in the STL standard library uses this syntax.
Use malloc to allocate memory P. Then, construct your own class Object new (p) T1 (value) in the memory; T1 is the constructed type, and value is the type value.
Allocator in my engine uses this method .... --
2. Naked Function
When your function is relatively simple, you cannot use too many registers. Or you need to decide some register variables based on the context. You can use the naked function. For general functions, if necessary, the compiler will generate code to save the ESI, EDI, EBX, and EBP registers when entering the function. When exiting the function, the code will generate code to restore the contents of these registers. Naked call does not generate such code. With the naked function, we can write clean functions by ourselves, especially when your code does not use these registers. Some operations can be omitted like inline functions.
Void _ declspec (naked) mynakedfunction ()
{
// Naked functions must provide their own Prolog.
_ ASM
{
Push EBP
MoV ESP, EBP
Sub ESP, _ local_size
}
// And we must provide epilog.
_ ASM
{
Pop EBP
RET
}
}
3. pointer to a class member function
In the MFC Library, many basic window classes call the message processing functions of the derived classes. Because Windows has a large number of messages, if you set up virtual functions for each message, and each virtual function occupies 4 bytes, many classes will be very large. In addition, in some cases, when designing the base class, consider that the derived class may have to be processed here. If you do not want to use virtual functions, you can also use pointers to member functions. The following class. If the derived class does not modify the PF member, the (* PF) () in the release will be optimized. In addition, you do not need to specify the dead function name to use pointers to member functions.
Class animate
{
Typedef void (Animate: * pfun) (void );
Public:
Animate (void) {pF = static_cast <pfun> (dosomething );}
Void sleep (void)
{
Cout <"animate sleep" <Endl;
(* PF )();
}
Void dosomething (void ){}
Pfun PF;
};
Class fish:
Public animate
{
Public:
Fish (void) {pF = static_cast <pfun> (TEST );}
Void test ()
{
Cout <"using a base class pointer to call a derived class non-virtual function" <Endl;
}
};
Int _ tmain (INT argc, _ tchar * argv [])
{
Animate * FS = new fish;
FS-> sleep ();
Delete FS;
Return 0;
}
4. _ declspec (novtable)
C ++ does not provide keywords like interfaces to define interfaces, but Mircrosoft C ++ provides _ declspec (novtable) to modify a class, to indicate that this class does not have a virtual function table, that is, all virtual functions (should) are pure virtual.
The declared base class does not have vtable (virtual function table), but still has vptr pointer (pointing to the virtual function table. Therefore, an error occurs when A1 * A = new A1; A-> Hello () is executed. The B1 derived from A1 has a vtable. So A1 * A = new B1; A-> Hello () will call the hello function of B1. If B1 does not define the hello function, the A1 function will be called. In fact, it is still the function address of the index in the vtable directed by the vptr of the B1 object. During compilation, because the hello virtual function is not defined in B1, the virtual function hello in vtable points to the A1 Class Hello function address. A good object-oriented design should be implemented by a derived class. The first Object-oriented Rule is interface-based programming. Therefore, all a1 functions are defined as pure virtual functions. A1 is equivalent to an interface and is implemented by the derived class B1. In this way, the abstraction and implementation are decoupled, which is more in line with object-oriented specifications.
The Class A1 defined in this way cannot call virtual functions. So the A1 Class should be regarded as an interface. A1 Class objects should not be generated. Similarly, when the program is generated, the A1 vtable space is saved. If there are a large number of virtual functions, there is still a considerable number of functions to save space * 4.
This keyword is owned by VC and is not supported by other C ++ compilers.
Class _ declspec (novtable) A1
{
Public:
A1 (void ){}
Virtual void Hello (void)
{
Cout <"a hello" <Endl;
}
};
Class B1: Public A1
{
Public:
B1 (void ){}
Void Hello (void)
{
Cout <"B Hello" <Endl;
}
};
Most of the above materials are from the Internet... Share --