Language design is the database design-Chapter 26th of "C ++ meditation"

Source: Internet
Author: User

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.

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.