Handle class (thinking in C ++ _ 1)

Source: Internet
Author: User
Requirements:

1) security technical security needs. Even if the core implementation is not visible in the library, some internal information is still exposed in the variable definition in the header file;

2) the need to save Compilation Time. At the initial stage of development and design, the implementation needs to be changed frequently, and variable definitions in the header files need to be changed frequently. Therefore, the header files need to be compiled during re-compilation, which sometimes leads to a long Compilation Time.

Solution:

The handle class can solve the above problems:

// : Handle. h -- handle classes
# Ifndef handle_h _
# Define Handle_h _

Class Handle {
StructCheshire;//Class declaration only
Cheshire*Smile;
Public:
Handle ();
Void doit ();
~ Handle ();
} ;
# Endif // Handle_h _

This is for all customersProgramAs you can see, struct Cheshire is a type description or class description that is not completely specified and will be used to store real variables. It tells the compiler that Cheshire is the name of a structure, but does not provide anything about the structure. This is sufficient for generating a pointer to the structure.

In this technique, the structure entity that contains the specific implementation is hidden in the implementation file.

// : Handle. cpp -- handle implementation
# Include " Handle. h "

// Define handle's implementation
Struct Handle: Cheshire {
IntI;
} ;

Handle: handle () {
Smile=(Cheshire*) Malloc (Sizeof(Cheshire ));
Smile->I=0;
}

Void Handle: doit () {
//Do something with I
}

Handle :: ~ Handle () {
Free (SMILE );
}


The use of the handle class is the same as that of any class, including header files, object creation, and sending information. However, with this design, the variable design is hidden, and the header file does not need to be re-compiled during changes. Bruce said that although this is not perfect information concealment, it is a great improvement after all.

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.