ABSTRACT Data Type
One of the requirements for designing a good library is to thoroughly isolate interfaces and implementations. c ++ uses constructor, destructor, and member functions:
- One of the basic methods to separate interfaces from implementations in C ++ is to use constructor and destructor. The constructor itself provides a method to generate a given class object, while the Destructor provides behavior with the constructor idea.
- Member functions prevent users from accessing class members they should not see.
Type secure link
When using the C library, a function may exist in multiple libraries. For example, SQRT (double) may exist in the math. h header file,
Extern double SQRT (double );
SQRT (complex) is in the complex library file complex. H,
Extern complex SQRT (complex );
In this case, you can use this declaration syntax to solve the problem:
Extern "C" Double SQRT (double );
Extern "C" complex SQRT (complex );
The exact meaning of the processing of extern "C" depends on the method in which the specific system processes the C function.
Namespace
Namespace solves a problem that is very prominent in C and increasingly serious in C ++: how to prevent different library designers from using the same name for their respective components.
Essentially, a namespace allows the library designer to specify a wrapper for all names that will be directed to the global scope by the library side. There are two usage methods:
For example, the library namespace of shadowsoft is named shadowsoft, which defines the string S. One usage is to use the namespace ID name:
Shadow: String S;
One is to reference a namespace:
Using namespace shadowsoft;
String S;
Memory Allocation
This section describes a seemingly conflicting memory allocation case:
Class Foo {
Public:
Void * operatornew (size_t );
Void * operator Delete (void *);
};
This class defines a set of memory management solutions (new and delete). Now, whenever you want to create or release a foo object in the memory, you have to use FOO :: operator new and foo: Operator Delete to dynamically allocate memory. What if we apply for memory through the system memory allocation function and create a foo object in the memory? For example:
Void * P = malloc (1024 );
Foo * TP = new (p) Foo;
The answer is that the container has priority. New (p) Foo will assign a foo object to the memory address specified by P, even if the class Foo has its own memory distributor.
Assign values and initialize values by Members
If a struct member has a complex type member, for example, a class, we do not know whether the value assignment process is bitwise replication or other methods, we can display the structure and replication.
Struct person
{
String name;
String address;
String telno;
Person ();
Person (const person & P): Name (P. Name), address (P. Address), telno (P. telno ){}
Person operator = (const person & P)
{
Name = P. Name;
Address = P. address;
Telno = P. telno;
Return * this;
}
};
Note: There is no need to give the person Class A displayed destructor, because the compiler will correctly inherit the Destructor from the string class destructor.